Count work days between two dates

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

    Here is a version that works well (I think). Holiday table contains Holiday_date columns that contains holidays your company observe.

    DECLARE @RAWDAYS INT
    
       SELECT @RAWDAYS =  DATEDIFF(day, @StartDate, @EndDate )--+1
                        -( 2 * DATEDIFF( week, @StartDate, @EndDate ) )
                        + CASE WHEN DATENAME(dw, @StartDate) = 'Saturday' THEN 1 ELSE 0 END
                        - CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END 
    
       SELECT  @RAWDAYS - COUNT(*) 
         FROM HOLIDAY NumberOfBusinessDays
        WHERE [Holiday_Date] BETWEEN @StartDate+1 AND @EndDate 
    

提交回复
热议问题