Count work days between two dates

前端 未结 24 2731
失恋的感觉
失恋的感觉 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:50

    I found the below TSQL a fairly elegant solution (I don't have permissions to run functions). I found the DATEDIFF ignores DATEFIRST and I wanted my first day of the week to be a Monday. I also wanted the first working day to be set a zero and if it falls on a weekend Monday will be a zero. This may help someone who has a slightly different requirement :)

    It does not handle bank holidays

    SET DATEFIRST 1
    SELECT
    ,(DATEDIFF(DD,  [StartDate], [EndDate]))        
    -(DATEDIFF(wk,  [StartDate], [EndDate]))        
    -(DATEDIFF(wk, DATEADD(dd,-@@DATEFIRST,[StartDate]), DATEADD(dd,-@@DATEFIRST,[EndDate]))) AS [WorkingDays] 
    FROM /*Your Table*/ 
    

提交回复
热议问题