Code for populating the 'date' dimension

Code for populating the 'date' dimension

Code for populating the 'date' dimension



For context, please see last tip .

 create procedure Load_Date_Dimension_Sp @year_start int, @year_end int as begin declare @yr int set @yr = year_start while (@yr <= year_end) begin insert into year_d (the_year) values (@yr) set @yr = @yr + 1 end declare @qt int set @qt = 1 while (@qt <= 4) begin insert into quarter_d (the_quarter) values (@qt) set @qt = @qt + 1 end declare @mo int set @mo = 1 while (@mo <= 12) begin insert into month_d (the_month) values (@mo) set @mo = @mo + 1 end update month_d set quarter_key = (select quarter_key from quarter_d where the_quarter=1) where the_month in (1,2,3) update month_d set quarter_key = (select quarter_key from quarter_d where the_quarter=2) where the_month in (4,5,6) update month_d set quarter_key = (select quarter_key from quarter_d where the_quarter=3) where the_month in (7,8,9) update month_d set quarter_key = (select quarter_key from quarter_d where the_quarter=4) where the_month in (10,11,12) declare @wk int set @wk = 1 while (@wk <= 52) begin insert into week_d (the_week) values (@wk) set @wk = @wk + 1 end declare @dt1 int set @dt1 = 1 while (@dt1 <= 31) begin insert into day_d (the_day) values (@dt1) set @dt1 = @dt1 + 1 end declare @dt datetime declare @dt_end = ?12/31/? + @year_end declare @day_k

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning editorial writers on the latest customer relationship management (CRM)and call center technology issues today. Our goal is to keep you informed on the hottest issues facing this fast-changing industry.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchCRM.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchCRM.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

int declare @week_k int declare @month_k int declare @year_k int set @dt = '01/01/? + @year_start while (@dt <= @dt_end) begin select @year_k=year_key from year_d where the_year = year(@dt) select @month_k=month_key from month_d where the_month = month(@dt) select @day_k=day_key from day_d where the_day = day(@dt) select @week_k=week_key from week_d where the_week = datepart(wk,@dt) insert into date_d (the_date, day_key, week_key, month_key, year_key) values (@dt, @day_k, @week_k, @month_k, @year_k) set @dt = @dt + 1 end end


This was first published in April 2002

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.

    Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.