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 Programming
 Setting DateTime variable with a Case

Author  Topic 

tjwent69
Starting Member

30 Posts

Posted - 2008-09-02 : 14:54:27

Can I use a case to set datetime variables based off of the value of another variable?



Variables

@Year1Start as DateTime --2006
@Year1End as DateTime --2006
@Year2Start as DateTime --2007
@Year2End as DateTime --2007
@Year3Start as DateTime --2008
@Year3End as DateTime --2008
@x as int -- zero or one

I prompt the user for the current start and End dates

These are @Year3Start @Year3End. The other dates are calculated from these.

So my history would sum(TotalSales)
Where Dates >= StartDates and < EndDates


For the TY vs LY for the Second Quarter here is how I do the dates.

Set @Year3StartDate = '04/01/2008'
Set @Year3EndDate = '06/30/2008' -- I do a dateadd in the query
Set @Year2StartDate = DateAdd(yyyy, -1, @Year3StartDate)
-- 04/01/2007
Set @Year1StartDate = DateAdd(yyyy, -2, @Year3StartDate)
-- 04/01/2006
Set @Year2EndDate = DateAdd(dd,1,(DateAdd(yyyy, -1, @Year3EndDate)))
-- 07/01/2007
Set @Year1EndDate = DateAdd(dd,1,(DateAdd(yyyy, -2, @Year3EndDate)))
-- 07/01/2006

For Year totals here is how I do the dates.

Set @Year3StartDate = '01/01/2008'
Set @Year3EndDate = '08/31/2008' -- I do a dateadd in the query
Set @Year2StartDate = DateAdd(yyyy, -1, @Year3StartDate)
-- 01/01/2007
Set @Year1StartDate = DateAdd(yyyy, -2, @Year3StartDate)
-- 01/01/2006
Set @Year2EndDate = @Year3StartDate
-- 01/01/2008
Set @Year1EndDate = @Year2StartDate
-- 01/01/2007

The start dates just subtract the appropriate number of years.

The End dates need to be different. I was hoping to do a case statement and if @x was a 1 then do the dates like TY vs LY and a
zero would to the totals.


   

- Advertisement -