Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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
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?

Question by:jon100
  • 3
  • 3
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)
LVL 75

Expert Comment

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

Expert Comment

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

ID: 18865009
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)
12-jan-2007       becomes     06-Apr-2007
12-Jul-2007       becomes     12-Jul-2008
24-Feb-2006     becomes     06-Apr-2007
LVL 75

Accepted Solution

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

Declare @NumberOfMonths integer

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

Author Comment

ID: 18867592
gerat thanks.

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

rather than a select.

Author Comment

ID: 18867737
great thanks for your help.

Think i got it sorted!

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

856 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