Solved

set variable to go back one year from today

Posted on 2015-01-30
6
108 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

In this article I will describe the Backup & Restore 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now