• Excellent function!!

     

    But as others have pointed out holidays will invariably need to be taken into account; we have a holiday table which (simplified version) is called HCD and has one field HolidayDate (DateTime). You simply need to add a simple select calc to the end of your functions thusly:

    --Start with total number of days including weekends

                    (DATEDIFF(dd,@StartDate,@EndDate)+1)

                  --Subtact 2 days for each full weekend

                   -(DATEDIFF(wk,@StartDate,@EndDate)*2)

                  --If StartDate is a Sunday, Subtract 1

                   -(CASE WHEN DATENAME(dw,@StartDate) = 'Sunday'

                          THEN 1

                          ELSE 0

                      END)

                  --If EndDate is a Saturday, Subtract 1

                   -(CASE WHEN DATENAME(dw,@EndDate) = 'Saturday'

                          THEN 1

                          ELSE 0

                      END)

           --New calc to account for Holidays added here        

        - (SELECT COUNT(*) FROM HCD

          WHERE    HolidayDate BETWEEN @StartDate AND @EndDate)

       &nbsp

    This should not detract from the original elegance of the solution as this is a simple select, not requiring cursors/loops