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.
Author |
Topic |
cardgunner
326 Posts |
Posted - 2008-06-26 : 08:45:01
|
I have a working statement, however I have a gut feeling that it may run into problems, maybe not.I need to call up a year ago last month. So it being June 2008 now, I need to call up May 2007.declare @s_date datetimedeclare @e_date datetimedeclare @LastMonth_Month intdeclare @LastMonth_Year intset @LastMonth_Month=5set @LastMonth_Year=2008set @s_date=dateadd(month,cast(@LastMonth_Month as int)-1,dateadd(year,cast(@LastMonth_Year as int)-1900,0))set @e_date=dateadd(month,cast(@LastMonth_Month as int),dateadd(year,cast(@LastMonth_Year as int)-1900,0)) The above code gets me May 2008 as NOW.@s_date='2008-05-01 00:00:00.000'@e_date='2008-06-01 00:00:00.000'I'm using the code below to get last year last month as THEN@s_date-365.25 to get '2007-05-01 00:00:00.000' and @e_date-365.25 to get '2007-06-01 00:00:00.000'I use @s_date and @e_date roughly 80 times thoughout my statement to get: 5/1/2008 to 06/01/2008 NOW5/1/2007 to 06/01/2007 THEN01/01/2008 to 06/01/2008 YTD05/01/2007 to 01/01/2008 LSTYRIs using -365.25 not correct? If it's not have you a better solution.CardGunner |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-06-26 : 09:25:19
|
use this to find the last year's dateselect start_date = dateadd(month, datediff(month, 0, getdate()) - 13, 0), end_date = dateadd(month, datediff(month, 0, getdate()) - 12, -1) KH[spoiler]Time is always against us[/spoiler] |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-06-26 : 10:29:03
|
You can also simplify your logic for "NOW":select LastMonthStart = dateadd(month,datediff(month,0,getdate())-1,0), CurrentMonthStart = dateadd(month,datediff(month,0,getdate()),0)Results:LastMonthStart CurrentMonthStart----------------------- ----------------------- 2008-05-01 00:00:00.000 2008-06-01 00:00:00.000(1 row(s) affected) CODO ERGO SUM |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-26 : 11:00:55
|
Thanks for the reply I will try to simplifing my NOW.However I'm using 5 for month and 2008 for year and I have to convert that to the getdate().What I take from this is that I will have to add to my declare and variableswhere it will be something like declare @LastMonth_Month intdeclare @LastMonth_Year intdeclare @now_s_date datetimedeclare @now_e_date datetimedeclare @thn_s_date datetimedeclare @thn_e_date datetimedeclare @ytd_s_date datetimedeclare @ytd_e_date datetimedeclare @lyr_s_date datetimedeclare @lyr_e_date datetimeselect @LastMonth_Month=5select @LastMonth_Year=2008select @now_s_date=dateadd(month,cast(@LastMonth_Month as int)-1,dateadd(year,cast(@LastMonth_Year as int)-1900,0))select @now_e_date=dateadd(month,cast(@LastMonth_Month as int),dateadd(year,cast(@LastMonth_Year as int)-1900,0))select @thn_s_date=dateadd(month,cast(@LastMonth_Month as int)-1,dateadd(year,cast(@LastMonth_Year as int)-1901,0))select @thn_e_date=dateadd(month,cast(@LastMonth_Month as int),dateadd(year,cast(@LastMonth_Year as int)-1901,0))select @ytd_s_date=select @ytd_e_date=select @lyr_s_date=select @lyr_e_date= which you will notice I didn't put in tfor ytd and lyr but will.I also went with select instead of set because I was told by a forum it was a better performer among other things.However is this better to do it this way or to do 2 variables and change that variable to meet the different requirements.I'm defining the code below is a variable declare @now_s_date datetimeset @now_s_date=dateadd(month,cast(@LastMonth_Month as int)-1,dateadd(year,cast(@LastMonth_Year as int)-1900,0)) Is it better to have 10 variables in 80 placesor4 variables altered in the statement in 80 places such as (@now_s_date -365.25) for THEN where it would go like t_rsdt >= @now_s_date-365.25 ?CardGunner |
 |
|
|
|
|
|
|