Solved

set variable to go back one year from today

Posted on 2015-01-30
6
121 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
[X]
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
  • 4
  • 2
6 Comments
 
LVL 66

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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

Author Comment

by:spoye
ID: 40580617
I have it now thanks


declare  @sdate         datetime,        
         @edate         datetime

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

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

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.

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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

695 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