Add business days to date in SQL without loops

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

    WITH get_dates
    AS
    (
        SELECT getdate() AS date, 0 as DayNo 
        UNION ALL
        SELECT date + 1 AS date, case when DATEPART(DW, date + 1) IN (1,7) then DayNo else DayNo + 1 end
        FROM get_dates
        WHERE DayNo < 4
    )
    SELECT max(date) FROM get_dates
    OPTION (MAXRECURSION 0) 
    

提交回复
热议问题