Adding Days to a Date but Excluding Weekends

前端 未结 11 2170
萌比男神i
萌比男神i 2020-12-01 15:47

Given a date how can I add a number of days to it, but exclude weekends. For example, given 11/12/2008 (Wednesday) and adding five will result in 11/19/2008 (Wednesday) rath

11条回答
  •  隐瞒了意图╮
    2020-12-01 16:34

    This is better if anyone is looking for a TSQL solution. One line of code and works with negatives.

    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
    

提交回复
热议问题