Okay, I feel like I should be able to do this, but I have searched to no avail.
I need to show one of two different data sets depending on what day of the year it is. Specifically, if today is October 31 (or earlier,) I want to pull all entries from October 1 of last year to the end of current year. If today is after October 31, I want my data set to show entries from October 1 of the current year to the end of next year.
The code below is creating a simple “calendar” table variable that is storing all the months that exist in a specific dataset. Right now I am just limiting it to dates for current year. I would like to replace that with code to allow it to function as I stated above. I thought of using an IF statement, but I cannot figure out how to compare with getdate() day and month only.
DECLARE @calendar TABLE
( mon_name VARCHAR(10)
, mon_number INT
, yr INT
)
INSERT INTO @calendar
SELECT DATENAME(m,departure_date)
, MONTH(departure_date)
, YEAR(departure_date)
FROM trip_mission
WHERE departure_date <>'1/1/1900'
AND YEAR(departure_date) = YEAR(getdate())
GROUP BY DATENAME(m,departure_date)
, MONTH(departure_date)
, YEAR(departure_date)
A simplified form of the question may be how can I run some code only if today is <= October 31. It would be super easy if I could say <= October 31, 2012, but I need it to be dynamic so that it will flip over every year.
The following little trick will allow you to select different subsets based on what month it is:
The last argument forms the date of the 1st of October of the current year. The second argument controls whether we subtract 1 year or not. Assuming the current year to be 2012, here’s what we get depending on the current month