Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Change Syntax to pull last 5 years instead of 2

Author  Topic 

niozero
Starting Member

3 Posts

Posted - 2011-03-31 : 16:43:47
Would I just add a 3, 4 and 5 to the declare statement and do the same under the SELECT in order to pull data from the past 5 years instead of 2 years? I am trying to modify a stored proceedure and I need it to pull from the last 5 years, not 2 years. I also have an @startdate and @enddate under the parameters under the stored proceedure.





DECLARE @Comp1StartDate datetime,
@Comp2StartDate datetime,
@Comp1EndDate datetime,
@Comp2EndDate datetime


/*Set the comparison dates to look at the same time period for the previous two years*/

SELECT @Comp1StartDate = Dateadd(year, -1, @startdate), @comp2StartDate - dateadd(year, -2, @startdate),
@Comp1EndDate = dateadd(year, -1, @enddate), @comp2EndDate = dateadd(year, -2, @enddate)




Stuck in a rut!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-31 : 18:41:40
Hard to say without seeing more of the stored proc, but if I had to guess, I would say it is what I have highlighted in RED that you should look at. That -2 in there is subtracting 2 years, so if you change that to -5, that may be what you need.
DECLARE @Comp1StartDate datetime,
@Comp2StartDate datetime,
@Comp1EndDate datetime,
@Comp2EndDate datetime


/*Set the comparison dates to look at the same time period for the previous two years*/

SELECT @Comp1StartDate = Dateadd(year, -1, @startdate), @comp2StartDate = dateadd(year, -2, @startdate),
@Comp1EndDate = dateadd(year, -1, @enddate), @comp2EndDate = dateadd(year, -2, @enddate)
BTW, I think the minus sign after comp2StartDate should not be a minus sign, it should be an = sign.

Then again, I am guessing :--)
Go to Top of Page
   

- Advertisement -