Your code will not work correctly if the setting of DATEFIRST <> 7, the default for US english.set datefirst 7-- For Monday, 2009-01-19select DW1 = DATEPART(dw,'20090119')set datefirst 2select DW2 = DATEPART(dw,'20090119')Results:DW1 ----------- 2(1 row(s) affected)DW2 ----------- 7(1 row(s) affected)
It would not have this problem if you replaced this code:IF DATEPART(dw,@Counter) in (2,3,4,5,6)
with this code, because it returns the same result with any setting of DATEFIRST:IF datediff(dd,'17530101',getdate())%7 in (0,1,2,3,4)
Also, there seems to be no point to this code:SET @WorkingDays = @WorkingDays
CODO ERGO SUM