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
292 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

810 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