andrew4582

Dimension date table sql script

Aug 15th, 2010
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.16 KB | None | 0 0
  1. /*---------------------------------------------------------------------------*/
  2. /* Loads a Date Dimension                                                    */
  3. /*---------------------------------------------------------------------------*/
  4.  
  5. -- A few notes, this code does nothing to the existing table, no deletes
  6. -- are triggered before hand. Because the DateKey is uniquely indexed,
  7. -- it will simply produce errors if you attempt to insert duplicates.
  8. -- You can however adjust the Begin/End dates and rerun to safely add
  9. -- new dates to the table every year.
  10. --
  11. -- If the begin date is after the end date, no errors occur but nothing
  12. -- happens as the while loop never executes.
  13.  
  14. SET NOCOUNT ON -- turn off all the 1 row inserted messages
  15.  
  16. -- Hold our dates
  17. DECLARE @BeginDate DATETIME
  18. DECLARE @EndDate DATETIME
  19.  
  20. -- Holds a flag so we can determine if the date is the last day of month
  21. DECLARE @LastDayOfMon CHAR(1)
  22.  
  23. -- Number of months to add to the date to get the current Fiscal date
  24. DECLARE @FiscalYearMonthsOffset INT  
  25.  
  26. -- These two counters are used in our loop.
  27. DECLARE @DateCounter DATETIME    --Current date in loop
  28. DECLARE @FiscalCounter DATETIME  --Fiscal Year Date in loop
  29.  
  30. -- Set the date to start populating and end populating
  31. SET @BeginDate = '01/01/2008'
  32. SET @EndDate = '12/31/2010'
  33.  
  34. -- Set this to the number of months to add to the current date to get
  35. -- the beginning of the Fiscal year. For example, if the Fiscal year
  36. -- begins July 1, put a 6 there.
  37. -- Negative values are also allowed, thus if your 2010 Fiscal year
  38. -- begins in July of 2009, put a -6.
  39. SET @FiscalYearMonthsOffset = 6
  40.  
  41. -- Start the counter at the begin date
  42. SET @DateCounter = @BeginDate
  43.  
  44. WHILE @DateCounter <= @EndDate
  45.       BEGIN
  46.             -- Calculate the current Fiscal date as an offset of
  47.             -- the current date in the loop
  48.             SET @FiscalCounter = DATEADD(m, @FiscalYearMonthsOffset, @DateCounter)
  49.  
  50.             -- Set value for IsLastDayOfMonth
  51.             IF MONTH(@DateCounter) = MONTH(DATEADD(d, 1, @DateCounter))
  52.                SET @LastDayOfMon = 'N'
  53.             ELSE
  54.                SET @LastDayOfMon = 'Y'  
  55.  
  56.             -- add a record into the date dimension table for this date
  57.             INSERT  INTO [Dim].[Date]
  58.                     (
  59.                       [DateKey]
  60.                     , [FullDate]
  61.                     , [DateName]
  62.                     , [DateNameUS]
  63.                     , [DateNameEU]
  64.                     , [DayOfWeek]
  65.                     , [DayNameOfWeek]
  66.                     , [DayOfMonth]
  67.                     , [DayOfYear]
  68.                     , [WeekdayWeekend]
  69.                     , [WeekOfYear]
  70.                     , [MonthName]
  71.                     , [MonthOfYear]
  72.                     , [IsLastDayOfMonth]
  73.                     , [CalendarQuarter]
  74.                     , [CalendarYear]
  75.                     , [CalendarYearMonth]
  76.                     , [CalendarYearQtr]
  77.                     , [FiscalMonthOfYear]
  78.                     , [FiscalQuarter]
  79.                     , [FiscalYear]
  80.                     , [FiscalYearMonth]
  81.                     , [FiscalYearQtr]
  82.                     )
  83.             VALUES  (
  84.                       ( YEAR(@DateCounter) * 10000 ) + ( MONTH(@DateCounter)
  85.                                                          * 100 )
  86.                       + DAY(@DateCounter)  --DateKey
  87.                     , @DateCounter -- FullDate
  88.                     , CAST(YEAR(@DateCounter) AS CHAR(4)) + '/'
  89.                       + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2) + '/'
  90.                       + RIGHT('00' + RTRIM(CAST(DATEPART(dd, @DateCounter) AS CHAR(2))), 2) --DateName
  91.                     , RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2) + '/'
  92.                       + RIGHT('00' + RTRIM(CAST(DATEPART(dd, @DateCounter) AS CHAR(2))), 2)  + '/'
  93.                       + CAST(YEAR(@DateCounter) AS CHAR(4))--DateName
  94.                     , RIGHT('00' + RTRIM(CAST(DATEPART(dd, @DateCounter) AS CHAR(2))), 2) + '/'
  95.                       + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2)  + '/'
  96.                       + CAST(YEAR(@DateCounter) AS CHAR(4))--DateName
  97.                     , DATEPART(dw, @DateCounter) --DayOfWeek
  98.                     , DATENAME(dw, @DateCounter) --DayNameOfWeek
  99.                     , DATENAME(dd, @DateCounter) --DayOfMonth
  100.                     , DATENAME(dy, @DateCounter) --DayOfYear
  101.                     , CASE DATENAME(dw, '11/16/2009')
  102.                         WHEN 'Saturday' THEN 'Weekend'
  103.                         WHEN 'Sunday' THEN 'Weekend'
  104.                         ELSE 'Weekday'
  105.                       END --WeekdayWeekend
  106.                     , DATENAME(ww, @DateCounter) --WeekOfYear
  107.                     , DATENAME(mm, @DateCounter) --MonthName
  108.                     , MONTH(@DateCounter) --MonthOfYear
  109.                     , @LastDayOfMon --IsLastDayOfMonth
  110.                     , DATENAME(qq, @DateCounter) --CalendarQuarter
  111.                     , YEAR(@DateCounter) --CalendarYear
  112.                     , CAST(YEAR(@DateCounter) AS CHAR(4)) + '-'
  113.                       + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2) --CalendarYearMonth
  114.                     , CAST(YEAR(@DateCounter) AS CHAR(4)) + 'Q' + DATENAME(qq, @DateCounter) --CalendarYearQtr
  115.                     , MONTH(@FiscalCounter) --[FiscalMonthOfYear]
  116.                     , DATENAME(qq, @FiscalCounter) --[FiscalQuarter]
  117.                     , YEAR(@FiscalCounter) --[FiscalYear]
  118.                     , CAST(YEAR(@FiscalCounter) AS CHAR(4)) + '-'
  119.                       + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @FiscalCounter) AS CHAR(2))), 2) --[FiscalYearMonth]
  120.                     , CAST(YEAR(@FiscalCounter) AS CHAR(4)) + 'Q' + DATENAME(qq, @FiscalCounter) --[FiscalYearQtr]
  121.                     )
  122.  
  123.             -- Increment the date counter for next pass thru the loop
  124.             SET @DateCounter = DATEADD(d, 1, @DateCounter)
  125.       END
  126.  
  127. SET NOCOUNT ON -- turn the annoying messages back on
  128.  
  129. -- Select all rows inserted for the final year as a sanity check
  130. SELECT  *
  131. FROM    [Dim].[Date]
  132. WHERE DateKey > (YEAR(@EndDate) * 10000)
Advertisement
Add Comment
Please, Sign In to add comment