A million ways to do this, in most of my systems I have a date dimension table used for reporting and other various things that I want to filter by various date criteria. You can easily build a table like this using DATEPART & DATENAME functions of MSSQL.
Example Script (this is from some of my old code):
-- =============================================
-- Build Dimension Date Table
-- =============================================
DECLARE @StartDate as smalldatetime, @EndDate as smalldatetime
SET @StartDate = '04/01/2010'
SET @EndDate = '03/31/2011'
BEGIN
SELECT
DATEPART(dy, @StartDate) as DAY_OF_YEAR,
CASE
WHEN DATENAME(qq,@StartDate)-1=0 THEN
4
ELSE
DATENAME(qq,@StartDate)-1
END AS FISCAL_PERIOD,
DATENAME(m,@StartDate) AS MONTH_DESC,
DATEPART(m,@StartDate) AS MONTH_NUM,
DATEPART(qq,@StartDate) AS QUARTER_NUM,
CONVERT(smalldatetime, CONVERT(CHAR(10),@StartDate,110)) AS SALES_DATE,
REPLACE(CONVERT(CHAR(10),@StartDate,06),' ','-') AS SALES_DATE_SPL,
DATEPART(yy,@StartDate) AS YEAR_NUM,
DATEPART(d,@StartDate) AS DAY_OF_MONTH,
CASE
WHEN DATEPART(m,@StartDate)< 4 THEN
DATENAME(yy,@StartDate)-1
ELSE
DATENAME(yy,@StartDate)
END AS FISCAL_YEAR,
CASE
WHEN DATEPART(m,@StartDate)>3 THEN
DATEPART(m,@StartDate)-3
ELSE
12-(3-DATEPART(m,@StartDate))
END AS FISCAL_MONTH
INTO OLAP_DATE_DIMENSION
SELECT @StartDate = @StartDate + 1
END
WHILE (@StartDate <= @EndDate)
BEGIN
BEGIN
INSERT INTO OLAP_DATE_DIMENSION SELECT
DATEPART(dy, @StartDate) as DAY_OF_YEAR,
CASE
WHEN DATENAME(qq,@StartDate)-1=0 THEN
4
ELSE
DATENAME(qq,@StartDate)-1
END AS FISCAL_PERIOD,
DATENAME(m,@StartDate) AS MONTH_DESC,
DATEPART(m,@StartDate) AS MONTH_NUM,
DATEPART(qq,@StartDate) AS QUARTER_NUM,
CONVERT(smalldatetime, CONVERT(CHAR(10),@StartDate,110)) AS SALES_DATE,
REPLACE(CONVERT(CHAR(10),@StartDate,06),' ','-') AS SALES_DATE_SPL,
DATEPART(yy,@StartDate) AS YEAR_NUM,
DATEPART(d,@StartDate) AS DAY_OF_MONTH,
CASE
WHEN DATEPART(m,@StartDate)< 4 THEN
DATENAME(yy,@StartDate)-1
ELSE
DATENAME(yy,@StartDate)
END AS FISCAL_YEAR,
CASE
WHEN DATEPART(m,@StartDate)>3 THEN
DATEPART(m,@StartDate)-3
ELSE
12-(3-DATEPART(m,@StartDate))
END AS FISCAL_MONTH
END
SELECT @StartDate = @StartDate + 1
END
After you have the data you need (probably load 20 years, 10 back and 10 forward) all you have to do is select the data you want. Since you want current month back, I would do a query for everything with a YEAR/MONTH <= TODAY and >= OLDEST DATE ALLOWED.
Example:
SELECT *
FROM OLAP_DATE_DIMENSION
WHERE SALES_DATE <= CONVERT(CHAR(10),GETDATE(),110))
AND DAY_OF_MONTH = '1'
ORDER BY SALES_DATE DESC;
** This query against the table above (which is probably way over kill for what you need), will return the first day of each month for every month in the table prior to today. Then you can take the various date parts to create a pretty display label to show in your select (e.g. March 2010, February 2010, January 2010, etc...).
Hopefully this makes sense, the only long term "maintenance" task would be updating your table with addition years of data. Since it's only 365 day per/year, you could really load a ton of data with very little impact to your system. If your pages gets lots of hits, you can cache the results so the data is in memory!