?
Solved

Select next business day

Posted on 2011-10-25
3
Medium Priority
?
270 Views
Last Modified: 2012-05-12
Experts,
Below is a simple select I use for setting shipping dates. If an order is processed before 14:30 it selects today, anfter 14:30 it selects tomorrow.. pretty straight forward.

I want to modify this to consider weekdays. meaning if an order is processed on a Friday after 14:30, it should select Monday not Saturday as the answer.  Using SQL server Mgmt Studio 2005


IF
(Select DATEPART(HOUR, CURRENT_TIMESTAMP) ) >= 14
and
(Select DATEPART(MINUTE, CURRENT_TIMESTAMP) ) > 30
Select DATEADD(DAY, 1, CURRENT_TIMESTAMP)
ELSE
SELECT CURRENT_TIMESTAMP

0
Comment
Question by:JDCam
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37024803
create a calendar table:
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

and you select the next day from that table that is not saturday, sunday, or eventually marked as holiday, and you are done.
yes, you "might" be able to solve this with "pure" sql (even for the moving holidays), but why take all that work if a simple calendar table can solve this at "no cost" and be 100% sure to be ok
0
 
LVL 2

Accepted Solution

by:
Eric3141 earned 2000 total points
ID: 37025799
IF (Select DATEPART(HOUR, CURRENT_TIMESTAMP) ) >= 14 and
   (Select DATEPART(MINUTE, CURRENT_TIMESTAMP) ) > 30)
BEGIN
   if (select datepart(weekday, getdate()) ) = 1  -- Sunday
        Select DATEADD(DAY, 1, CURRENT_TIMESTAMP);

      if (select datepart(weekday, getdate()) ) = 7  -- Saturday
        Select DATEADD(DAY, 2, CURRENT_TIMESTAMP);

     if (select datepart(weekday, getdate()) ) NOT IN (1, 7)  -- Mon - Fri
        Select DATEADD(DAY, 1, CURRENT_TIMESTAMP);

END
0
 
LVL 1

Author Closing Comment

by:JDCam
ID: 37025879
thanks Eric3141.
Had to modify a bit, but works fine


IF
      (select datepart(weekday, current_timestamp) ) = 7
SELECT DATEADD(DAY, 2, CURRENT_TIMESTAMP)
ELSE
IF
      (select datepart(weekday, current_timestamp) ) = 1
SELECT DATEADD(DAY, 1, CURRENT_TIMESTAMP)
ELSE
IF
      (Select DATEPART(HOUR, CURRENT_TIMESTAMP) ) >= 14
      and
    (Select DATEPART(MINUTE, CURRENT_TIMESTAMP) ) > 30
      and
      (select datepart(weekday, current_timestamp) ) = 6
SELECT DATEADD(DAY, 3, CURRENT_TIMESTAMP)
ELSE
IF
      (Select DATEPART(HOUR, CURRENT_TIMESTAMP) ) >= 14
      and
    (Select DATEPART(MINUTE, CURRENT_TIMESTAMP) ) > 30
      and
      (select datepart(weekday, current_timestamp) ) in (2,3,4,5)
SELECT DATEADD(DAY, 1, CURRENT_TIMESTAMP)
ELSE
SELECT CURRENT_TIMESTAMP
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question