A record set of calendar dates with dimensional attributes can often come in handy. Leveraging the utfNumbers function, such a record set is easily constructed. The view below will return every calendar date between 1 Jan 1900 and 31 Dec 2100 — a full two-hundred years. Of course, it is easily modified to return more (or fewer) records.

If your application is such that join performance is an issue, you can simply materialize the view as a table or index the view (which results in transparent materialization).

-- Create a record set of dates with dimensional attributes
CREATE VIEW dbo.vwDates
    , CalendarDay = CONVERT(TINYINT, DATEPART(DAY, Dates.CalendarDate))
    , WeekDayInt = CONVERT(TINYINT, DATEPART(WEEKDAY, Dates.CalendarDate))
    , NameDay = DATENAME(WEEKDAY, Dates.CalendarDate)
    , CalendarMonth = CONVERT(TINYINT, DATEPART(MONTH, Dates.CalendarDate))
    , NameMonth = DATENAME(MONTH, Dates.CalendarDate)
    , CalendarYear = CONVERT(SMALLINT, DATEPART(YEAR, Dates.CalendarDate))
    DB.dbo.utfNumbers(DATEDIFF(DAY, '1/1/1900', '1/1/2100')) AS Nums
        SELECT CONVERT(DATE, DATEADD(DAY, Nums.n, '12/31/1899')) AS CalendarDate
    ) AS Dates

Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: