Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:39:17+00:00 2026-06-12T18:39:17+00:00

I have seen a number of postings regarding how to create an SQL Server

  • 0

I have seen a number of postings regarding how to create an SQL Server function that will add a given number of working days to a date. But, none of them calculate exactly the way I need. We have a function that is currently done in code but that I want to move to a native SQL Server function (for use by stored procedures and in queries). We give our customers 5 working days to collect cargo before we start assessing storage charges. The 5 working days exclude weekends and holidays (we have a table with the holiday dates). The trick here is that I need to get the date immediately after the 5 working days regardless of whether it is a weekend or holiday. So I need the function to return the last working date, NOT the first working day after. So, for example:

Oct 20th (Sat) plus 5 working days = Oct 26th (Fri)
Oct 21st (Sun) plus 5 working days = Oct 26th (Fri)
Oct 22nd (Mon) plus 5 working days = Oct 29th (Mon)
May 19th (Sat) plus 5 working days with May 21st a holiday = May 28th

The 5 working days is the current allotment, but this may change in the future so the number of working days needs to be a parameter. Also, the function may be used over fairly large datasets so I would prefer doing this without loops. We are running SQL Server 2008.

Edit: This is not a duplicate of “Add business days to date in SQL without loops” as they want the ending date to be a working day. I want my ending date to be whatever date immediately follows the last grace day (ie: 5 working days Mon to Fri I want the Sat date returned, NOT the following Mon).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:39:18+00:00Added an answer on June 12, 2026 at 6:39 pm
    create table holidays (
      date date);
    GO
    
    create function dbo.findWorkDayAfter(@date datetime, @days int)
    returns date as
    begin
    return (
      select thedate
      from (
      select thedate=dateadd(d,v.day,cast(@date as date)),
             rn=row_number() over (order by v.day)
      from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day)
      left join holidays h on h.date = dateadd(d,v.day,cast(@date as date))
      where h.date is null and left(datename(dw,dateadd(d,v.day,cast(@date as date))),1) <> 'S'
      ) x
      where @days = rn
      )
    end
    GO
    

    Unless you have long holidays, 10 days should be enough to find the 5th next working day. Increase it if you need to.

    If you need a larger number of business days from a date, you can use this which will cater for a year or three.

    alter function dbo.findWorkDayAfter(@date datetime, @days int)
    returns date as
    begin
    return (
      select thedate
      from (
      select thedate=dateadd(d,v.number,cast(@date as date)),
             rn=row_number() over (order by v.number)
      from master..spt_values v
      left join holidays h on h.date = dateadd(d,v.number,cast(@date as date))
      where h.date is null and left(datename(dw,dateadd(d,v.number,cast(@date as date))),1) <> 'S'
        and v.number >= 1 and v.type='p'
      ) x
      where @days = rn
      )
    end
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen that a prime number implementation of the GetHashCode function is being
I have seen a number of C# roles that specifically require WinForms experience. However
I have seen a number of posts here that describe how to parse HTML
i have seen a few sites like prankdial that lets you spoof your number
On a number of machines (with Asp.net 1 and 2) we have seen the
I have seen some people in SO commenting that Singleton Pattern is an anti-pattern.
I have seen behaviour in Perforce that I am unable to explain. I took
I have seen number of frameworks implement DI in php. However, since all objects
In looking through samples I have seen a number of examples where it is
I have seen a couple threads about what everyone was doing now a days

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.