Count work days between two dates

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

     DECLARE @TotalDays INT,@WorkDays INT
     DECLARE @ReducedDayswithEndDate INT
     DECLARE @WeekPart INT
     DECLARE @DatePart INT
    
     SET @TotalDays= DATEDIFF(day, @StartDate, @EndDate) +1
     SELECT @ReducedDayswithEndDate = CASE DATENAME(weekday, @EndDate)
      WHEN 'Saturday' THEN 1
      WHEN 'Sunday' THEN 2
      ELSE 0 END 
     SET @TotalDays=@TotalDays-@ReducedDayswithEndDate
     SET @WeekPart=@TotalDays/7;
     SET @DatePart=@TotalDays%7;
     SET @WorkDays=(@WeekPart*5)+@DatePart
    
     RETURN @WorkDays
    

提交回复
热议问题