Add business days to date in SQL without loops

后端 未结 25 2757
无人共我
无人共我 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:44

    This is what I use:

    SET DATEFIRST 1;
    
    SELECT DATEADD(dw, (**NumberToAdd**/5)*7+(**NumberToAdd** % 5) + 
    (CASE WHEN DATEPART(dw,**YourDate**) + (**NumberToAdd** % 5) > 5 
    THEN 2 ELSE 0 END), **YourDate**) AS IncrementedDate
    FROM YourTable t
    

    The "SET DATEFIRST 1;" part is necessary to set Monday as the first day of the week.

    0 讨论(0)
提交回复
热议问题