Count work days between two dates

前端 未结 24 2676
失恋的感觉
失恋的感觉 2020-11-22 01:05

How can I calculate the number of work days between two dates in SQL Server?

Monday to Friday and it must be T-SQL.

24条回答
  •  隐瞒了意图╮
    2020-11-22 01:36

    (I'm a few points shy of commenting privileges)

    If you decide to forgo the +1 day in CMS's elegant solution, note that if your start date and end date are in the same weekend, you get a negative answer. Ie., 2008/10/26 to 2008/10/26 returns -1.

    my rather simplistic solution:

    select @Result = (..CMS's answer..)
    if  (@Result < 0)
            select @Result = 0
        RETURN @Result
    

    .. which also sets all erroneous posts with start date after end date to zero. Something you may or may not be looking for.

提交回复
热议问题