Weekdays between two days

  • I had found some elegant code posted here to calculate the number of days minus weekends between two given dates. And, I cannot locate it now.

    Can someone post it if they a script to do it or point to the right area?

  • This will do it

    DECLARE @StartDate DATETIME

    DECLARE @EndDate DATETIME

    DECLARE @Cnt INT

    DECLARE @Diff INT

    DECLARE @OutCnt INT

    SET @StartDate = '2/1/2002' --Starting point

    SET @EndDate = '3/1/2002' --Ending Point but does not count this day change WHILE @Cnt <= @Diff to count day

    SET @Cnt = 0 --Set TO 0 so NULL has no effect on it.

    SET @OutCnt = 0 --Set TO 0 so NULL has no effect on it.

    SET @Diff = DATEDIFF(d,@StartDate, @EndDate) --Calc number of days

    PRINT @Diff

    WHILE @Cnt < @Diff --As long as we haven't gone that many days.

    BEGIN

    IF DATEPART(dw,DATEADD(d,@Cnt, @StartDate)) IN (2,3,4,5,6)

    SET @OutCnt = @OutCnt + 1

    SET @Cnt = @Cnt + 1 --Add one day TO incrementing value

    END

    PRINT @OutCnt --Output COUNT OF buiness days.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply