?
Solved

Adding three months to a named variable in SQL Server

Posted on 2012-09-05
6
Medium Priority
?
373 Views
Last Modified: 2012-09-05
I have the following statement in SQL Server

c.row_created < '~endDate~'

Where ~endDate~ is a variable that is inputted as a run variable

Is there a way to add three months to endDate?  If the value of endDate is 12/31/2011.   I would like to compare to 3/31/2012.
0
Comment
Question by:morinia
  • 6
6 Comments
 
LVL 21

Expert Comment

by:oleggold
ID: 38368324
0
 
LVL 21

Accepted Solution

by:
oleggold earned 2000 total points
ID: 38368326
0
 
LVL 21

Expert Comment

by:oleggold
ID: 38368330
in Your case:
SELECT DATEADD(month, 3, '2001-12-31')
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 21

Expert Comment

by:oleggold
ID: 38368480
here's the entire test case:
0
 
LVL 21

Expert Comment

by:oleggold
ID: 38368482
declare @@tb_name varchar(277);
declare @datet datetime ;
declare @today datetime ;
set @today =GETDATE();
set @datet=DATEADD(month, 3, '2011-12-31') ; --GETDATE();
declare @diff integer
declare @username varchar(277);
set @username= ''+USER_NAME()+'';
declare @date varchar(20);
declare @d2 varchar(1);
set @date=substring(replace(cast( CONVERT(varchar(35), GETDATE(), 127) as varchar),'-','_'),0,14)--':','_')
set @diff= cast( CONVERT(varchar(35),DATEDIFF("mm",@datet ,@today),127 ) as integer)-- cast( CONVERT(integer),DATEDIFF("mm",@datet ,@today),127 ) as integer;
select @diff --DATEADD(month, 3, '2001-12-31')
if @diff > 0 --datediff(@datet,@today)>0 --(DT_DBTIMESTAMP)"12/12/2011"
 RAISERROR('More then 3 month passed ,please use valid date ',1,16,1)
return
0
 
LVL 21

Expert Comment

by:oleggold
ID: 38368483
result:
(1 row(s) affected)
More then 3 month passed ,please use valid date
Msg 50000, Level 1, State 16
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

830 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