SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Modifying A Date So That It Is Neither A Weekend Or Holiday?


Modifying A Date So That It Is Neither A Weekend Or Holiday?

Author
Message
skempf
skempf
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 18
I have a stored procedure that is used to sync dates between two databases. It looks at the project start date in one database and the production date for a specific component in the other database. If that production date is later than the project start date, it syncs the two dates.

The stored procedure is run at midnight each night. Component production is set at 2:30pm each day. So, for example, at 2:30pm on July 1st, the production for July 2nd is set. Thus it doesn't do any good to have the stored procedure set production dates to July 1st or July 2nd and instead should use July 3rd as the next available start date.

This issue is addressed by checking if the intended start date is at least one day in the future and, if not, changing the intended start date to be such.

What I'm not entirely sure how to go about doing is checking to ensure that the new intended start date isn't a Saturday, Sunday or Holiday.

Checking if the date is a Saturday or Sunday is easy enough using DATENAME(dw, @STARTDATE) and then adding 1 or 2 days to the date accordingly.

What I'm not entirely sure about is the proper way to continue adding days to the date until it's neither a Saturday no Sunday and also doesn't appear in the Holidays table.

What would be the proper structure of a loop to ensure the date is neither a weekend nor a holiday?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60415 Visits: 17954
skempf (6/26/2013)
I have a stored procedure that is used to sync dates between two databases. It looks at the project start date in one database and the production date for a specific component in the other database. If that production date is later than the project start date, it syncs the two dates.

The stored procedure is run at midnight each night. Component production is set at 2:30pm each day. So, for example, at 2:30pm on July 1st, the production for July 2nd is set. Thus it doesn't do any good to have the stored procedure set production dates to July 1st or July 2nd and instead should use July 3rd as the next available start date.

This issue is addressed by checking if the intended start date is at least one day in the future and, if not, changing the intended start date to be such.

What I'm not entirely sure how to go about doing is checking to ensure that the new intended start date isn't a Saturday, Sunday or Holiday.

Checking if the date is a Saturday or Sunday is easy enough using DATENAME(dw, @STARTDATE) and then adding 1 or 2 days to the date accordingly.

What I'm not entirely sure about is the proper way to continue adding days to the date until it's neither a Saturday no Sunday and also doesn't appear in the Holidays table.

What would be the proper structure of a loop to ensure the date is neither a weekend nor a holiday?


You should search this site for calendar tables. There are several great articles for this type of thing.


What would be the proper structure of a loop to ensure the date is neither a weekend nor a holiday?


There is no proper structure for a loop for this type of thing. A loop is not needed if you use a calendar table and set based queries.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Steve Jones
Steve Jones
SSC Guru
SSC Guru (142K reputation)SSC Guru (142K reputation)SSC Guru (142K reputation)SSC Guru (142K reputation)SSC Guru (142K reputation)SSC Guru (142K reputation)SSC Guru (142K reputation)SSC Guru (142K reputation)

Group: Administrators
Points: 142088 Visits: 19421
I'd build a calendar tables (there are articles on this site for that). essentially populate it with all days, mark weekends, holidays, and other days. That way you can join with that table and determine the next available "work" day.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search