Add business days to date in SQL without loops

后端 未结 25 2874
无人共我
无人共我 2020-12-02 22:54

I currently have a function in my SQL database that adds a certain amount of business days to a date, e.g. if you enter a date that is a Thursday and add two days, it will r

25条回答
  •  不思量自难忘°
    2020-12-02 23:43

    This is better if anyone is looking for a TSQL solution. No loops, no tables, no case statements AND works with negatives. Can anyone beat that?

    CREATE FUNCTION[dbo].[AddBusinessDays](@Date date,@n INT)
    RETURNS DATE AS 
    BEGIN
    DECLARE @d INT;SET @d=4-SIGN(@n)*(4-DATEPART(DW,@Date));
    RETURN DATEADD(D,@n+((ABS(@n)+@d-2)/5)*2*SIGN(@n)-@d/7,@Date);
    END
    

提交回复
热议问题