?
Solved

set variable to go back one year from today

Posted on 2015-01-30
6
Medium Priority
?
122 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:steven
[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 2000 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:steven
ID: 40580588
Thanks so Much!
0
 

Author Comment

by:steven
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:steven
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:steven
ID: 40580717
Thanks...
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…
Suggested Courses

762 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