Calculating days to excluding weekends (Monday to Friday) in SQL Server

后端 未结 5 774
死守一世寂寞
死守一世寂寞 2020-12-07 04:10

How can I calculate the number of work days between two dates from table (from the 1st row to the end) in SQL Server 2008?

I tried something like this, but it does

5条回答
  •  渐次进展
    2020-12-07 04:55

    Interesting question. It is always important to understand the use case. If counting from Sunday to Monday, would we want to say there is one day as if we had until close of business on Monday. Or would we want to say there are no days as if there were no days before Monday began. In our case we needed to count both days (start and end) if they were weekdays, because I was working in a payroll application estimating some accruals. And any day weekend days would later be accounted for by holiday, and overtime records.

    When I pulled out my calendar I realized that when counting from Saturday or Sunday we could just start counting from Monday. And also when counting to a Saturday or Sunday I could just stop counting when we got to Friday. So I wrote a function that adjusted the starting and ending dates, Found the number of weeks by dividing by 7, multiplied by that 5 weekdays per week and then added back the remainder. I did have to account for the case when we started counting on a weekend but never got to a Monday.

    -- ============================================= -- Author: Todd P Payne -- Create date: 9/1/2018 -- Description: Counts number of weekdays between two dates -- Unlike DateDiff StartDate and EndDate are inclusive -- FROM Monday, Jan 1 to Monday Jan 1 will return 1 -- ============================================= CREATE FUNCTION [dbo].[ufnCountWeekdays] ( -- Add the parameters for the function here @StartDate DateTime, @EndDate DateTime ) RETURNS INT AS BEGIN -- Declare the return variable here DECLARE @CountofWeekDays INT = NULL; DECLARE @TempDate DateTime;

    -- Could CountBackwords 
    IF @StartDate > @ENDDate
    BEGIN
        SET @TempDate = @StartDate; 
        SET @StartDate = @EndDate;
        SET @EndDate = @TempDate;
    END
    
    --Start on Weekend Never get to Monday Case
    IF (DatePart(dw,@StartDate) = 7 AND DateDiff(Day,@StartDate,@EndDate) < 2)
    OR (DatePart(dw,@StartDate) = 1 AND DateDiff(Day,@StartDate,@EndDate) < 1 )
    BEGIN 
        SET @CountOfWeekDays = 0 -- Never got to a WeekDay
    END
    
    --NORMAL CASE
    ELSE BEGIN 
     -- IF Sat Start just pretend Start Counting on Monday 
     IF (DatePart(dw,@StartDate) = 7) SET @StartDate = dateadd(Day, 2, @StartDate);
     -- IF Sun Start just Start to Counting on Monday 
     IF (DatePart(dw,@StartDate) = 1) SET @StartDate = dateadd(Day, 1, @StartDate);
     -- Sat End just Stop counting on Friday
     IF (DatePart(dw,@EndDate) = 7) SET @EndDate = DateAdd(Day, -1, @EndDate);
     -- Sun END
     if (DatePart(dw,@EndDate) = 1) SET  @EndDate = DateAdd(Day, -2, @EndDate);
     --Find the total number of days we need to count
     Declare @DaysToCount INT = DateDiff(Day,@StartDate, @EndDate)+1; --include start
     -- five days for each full week plus any other weekdays 
     -- remember no worries about starting or ending on weekends
      Set @CountofWeekDays  = Floor(@DaysToCount/7)*5 + (@DaysToCount % 7)
    
    END     
    
    --Check to see if we are counting backwards
    IF @TempDate = @EndDate SET @CountofWeekDays = -1 * @CountofWeekDays;
    
    RETURN @CountofWeekDays;
    

    END GO

    Happy Coding

    Todd P Payne

提交回复
热议问题