Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / September 2008

Tip: Looking for answers? Try searching our database.

How to set a date conditionally in SQL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul - 30 Sep 2008 22:23 GMT
Hi I have a stored procedure that inserted data into a database and also
inserts a due date.  I need the query to set the due date as follows;
If the 5th day of the month falls on a saturday I want to set the due date
to the friday prior (4th), if the 5th falls on a sunday I want to set the due
date on the following monday (6th).  Thanks.
Signature

Paul G
Software engineer.

Eric Isaacs - 30 Sep 2008 22:42 GMT
Use the DATEPART function to determine the weekday...

DECLARE @DueDate DATETIME
SET @DueDate = '2/4/2006' --use your current logic to determine the
correct due date.

SELECT @DueDate = CASE
                   WHEN DATEPART(weekday, @DueDate) = 7 THEN
                       DATEADD(DAY, -1, @DueDate)
                   WHEN DATEPART(weekday, @DueDate) = 1 THEN
                       DATEADD(DAY, 1, @DueDate)
                   ELSE
                       @DueDate
                 END

SELECT @DueDate

- Eric Isaacs
Plamen Ratchev - 30 Sep 2008 22:50 GMT
You can use a CASE expressions and the date/time functions to calculate
the due date (note this calculation is dependent on SET DATEFIRST):

SELECT CASE DATEPART(dw,
                     DATEADD(DAY, 4,
                     DATEADD(MONTH,
                     DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)))
            WHEN 1 -- Sunday
            THEN DATEADD(DAY, 5,
                 DATEADD(MONTH,
                 DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0))
            WHEN 7 -- Saturday
            THEN
                 DATEADD(DAY, 3,
                 DATEADD(MONTH,
                 DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0))
            ELSE DATEADD(DAY, 4,
                 DATEADD(MONTH,
                 DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0))
       END AS due_date;

Signature

Plamen Ratchev
http://www.SQLStudio.com

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.