SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Date and Time dimension creation and population T-SQL

By Joshua A. Walker, 2009/02/13

Total article views: 6690 | Views in the last 30 days: 175

Dim_Date and Dim_Time, Date and Time dimensions. Creation, population, indexing and use script.

Just copy the code into a query window and run the script.

There are no additional functions or procedures required.

This code will create the tables, populate the date and time tables, add holidays, add indexes and there are a few code snippets at the bottom for using them.

There are two variables in the code right after the table creation "@StartDate" and "@EndDate". Set the @StartDate to the first day you wish to use in your dim_Date table. By default this is 1/1/1900. Then set the @EndDate to the day after the last day you want to include. By default this is 1/1/2050. Every date from 1/1/1900 through 12/31/2049 will be included in your data.

The "ID" field on the dim_Date table is the primary key and the date as format YYYYMMDD. There are lines to make it an auto incrementing int that will start at 60,000 if you wish to follow the Kimball DW book that suggest not using a date format as your primary key.

The Date and Time columns in the tables are set up as character fields. This is the proper way to do this for reporting purposes. All day,month, hour, minute and second columns will always be two characters '01' vs. 1...etc...

All columns are indexed after the population.

For functions for these tables that will return the keys from the date and time tables when passed a datetime... and a function for returning the datetime when a key is passed... go here: http://www.sqlservercentral.com/scripts/Data+Warehouse/65897/

By Joshua A. Walker, 2009/02/13

Total article views: 6690 | Views in the last 30 days: 175
Your response
 
 
Related Articles
FORUM

Covering Index vs Included Columns

Is there any functional difference when querying

FORUM

user defined functions & columns

user defined functions & columns

FORUM

Index creation - Before OR AFTER Data?

Create index before data population or after?

FORUM

Difference between Composite Index and Single Column Indexes ?

Difference between Composite Index and Single Column Indexes ?

FORUM

More duration, less reads after indexing?

Indexing after wizard leaded to inverse result

Tags
data warehousing    
datawarehouse    
datetime    
dimensions    
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com