DateAdd for Workdays

  • I wonder if anyone can help me to calculate a date that will be let's say five days from another date but excluding weekends.

    Thanks,

    Jakub

  • Simple enough. Check the day (using datepart) and setup a case statement that will add 5, 6, 7, 8 days to the date to skip weekends.

    Steve Jones

    sjones@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/sjones

  • I am probably little dense, but 5,6,7,8 days?

    I am not sure what do you mean.

    What if the start day is friday and I need to add 2 days

    i.e. DateAdd(day, 2, 6/28/2002) would give me result 6/30/2002

    but since I need to exclude weekend days I need a function that will give me

    result 7/2/2002

    I know that I need to use the datepart somehow, but I am not sure how to structure it without iterating throug the entire time interval.

    But maybe there isn't any better solution.

    Thanks,

    Jakub

  • Hint:

    select case

    when datepart() = 2 -- Monday

    then dateadd( day, 5, @mydate)

    when datepart() = 3 -- Tuesday

    then dateadd( day, 7, @mydate)

    when datepart() = 3 -- Wednesday

    then dateadd( day, 7, @mydate)

    end

    You can fill in the blanks and verify how the days are classified on your server. Look up datepart() to get the day of the week syntax. I guess it's only 5 or 7 days you add, not 5, 6, 7, or 8.

    Steve Jones

    sjones@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/sjones

  • I got it now, slower wire here 🙂

    Thanks.

    It only works for the five day interval, if I wanted to build a generic function I would have to account for all possibilities. 🙁

    Thanks for the hint.

    Jakub

Viewing 5 posts - 1 through 4 (of 4 total)

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