Solved

set variable to go back one year from today

Posted on 2015-01-30
6
114 Views
Last Modified: 2015-01-30
I Need to insure that I go back one year from today. The variables are both datetime @sdate and @edate, we have the @sdate set to Getdate().  We need to set the @edate  to insure it goes back one year from today..can anyone help.
0
Comment
Question by:spoye
  • 4
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40580581
Something like ...
Declare @sdate datetime, @edate datetime

SELECT @sdate = GETDATE()
SELECT @edate = DATEADD(year, -1, @sdate) 

Open in new window

btw s and e implies Start and End, and in your example start is a year after end.  
Might be confusing.
0
 

Author Closing Comment

by:spoye
ID: 40580588
Thanks so Much!
0
 

Author Comment

by:spoye
ID: 40580597
SORRY THIS DIDN'T WORK Results below..can you please help.

declare  @sdate         datetime,        
         @edate         datetime
SELECT @sdate = GETDATE()
SELECT @edate = DATEDIFF(year, -1, @sdate)
select @sdate,@edate


RESULTS:
2015-01-30 15:52:01.247
1900-04-27 00:00:00.000
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:spoye
ID: 40580617
I have it now thanks


declare  @sdate         datetime,        
         @edate         datetime

SELECT @edate = dateadd(yy, -1, GETDATE())
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40580631
My bad, DATEDIFF should have been DATEADD.  Corrected in first post.
0
 

Author Comment

by:spoye
ID: 40580717
Thanks...
0

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

775 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