I’ve got a query and I want to add parameters to calculate the moving annual (the figures for the last 12 months). I’m trying to subtract 12 months from today, so if for instance today is 1 August 2012 then my @StartDate should be ‘2011-09-01’ and my @EndDate should be ‘2012-08-31’. So how do I change set my parameters to accommodate this?
declare @StartDate DATE
declare @EndDate DATE
SET @StartDate = DATEADD(MONTH, -12, '2012-08-01')
SET @EndDate = DATEADD(MONTH, +1, '2012-08-01')
EDIT:
However, I do not recomend using
'2011-09-01'to'2012-08-31'as a representation of a year. Instead, use'2011-09-01'to'2012-09-01'in the following way…This works for all Date and DateTime data types. Even if the value in
dateFieldis 2:30pm on 31st August, this will still work. It’s a one size fits all approach and makes it much more difficult to make mistakes on date and time boundaries.