Solved

easy SQL: if date field before today - make it X months from now. else make it X months from its value

Posted on 2007-04-06
7
271 Views
Last Modified: 2008-02-01
SQL 2000.

I have a datetime value - if it is after today - I need to add X months (int value passed into sproc) to it - otherwise I need to add X months to today's date.

can I do this in one sql statement? what is the most efficient option?


thanks!...
0
Comment
Question by:jon100
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18864693
Not following you, but take a look at the DATEADD() function.  To add X months you would do something like this:

SELECT DATEADD(month, @X, @YourDateTyimeValue)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18864706
Perhaps this is what you need:
SELECT CASE
                    WHEN @YourDateTimeValue > GETDATE() THEN DATEADD(month, @X, @YourDateTimeValue)
                    ELSE DATEADD(month, @X, GETDATE())
              END
0
 
LVL 9

Expert Comment

by:dan_neal
ID: 18864725
Have you looked at using CASE statements?
Such as:
CASE WHEN datefield > getdate() THEN dateadd(d,X,datefield) ELSE dateadd(d,x,getdate())

You can also use this to subtract days from the field or current date by inserting a negative number for X
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:jon100
ID: 18865009
hi
sorry if its not clear. Ive got the DATEADD thing down. If the date in a field is before todays date - set the date in that field to today +12 months. If its after todays date - set it to its value + 12 months (number of months may vary).
Let me give you an example

(today's date is 06-apr-2007)
table;
12-jan-2007       becomes     06-Apr-2007
12-Jul-2007       becomes     12-Jul-2008
24-Feb-2006     becomes     06-Apr-2007
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 125 total points
ID: 18865401
Than something like this:

Declare @NumberOfMonths integer

Set @NumberOfMonths = 12
SELECT CASE
                    WHEN YourDateTimeColumn > GETDATE() THEN DATEADD(month, @NumberOfMonths, YourDateTimeColumn)
                    ELSE DATEADD(month, @NumberOfMonths, GETDATE())
              END
From YourTableName
0
 

Author Comment

by:jon100
ID: 18867592
gerat thanks.

just one thing - i need to do an update - where an id = @id

rather than a select.
0
 

Author Comment

by:jon100
ID: 18867737
great thanks for your help.

Think i got it sorted!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now