This script generates approximately 20 years of data. It uses an intelligent surrogate key and begins counting at 2014-01-01. It generates date part names and their numeric values for sorting purposes.
SELECT -- 20 years, approximate TOP (20 * 365) CAST(CONVERT(char(8), D.FullDate, 112) AS int) AS DateKey , D.FullDate , YEAR(D.FullDate) AS YearValue , 'Q' + DATENAME(QUARTER, D.FullDate) AS QuarterName , DATEPART(QUARTER, D.FullDate) AS QuarterValue , DATENAME(mm, D.FullDate) AS MonthName , MONTH(D.FullDate) AS MonthValue , DAY(D.FullDate) AS DayValue , DATENAME(dw, D.FullDate) AS DayOfWeekName , DATEPART(dw, D.FullDate) AS DayOfWeekValue FROM ( SELECT DATEADD(d, D.number, BOT.StartDate) AS FullDate FROM ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1 AS number FROM sys.all_columns AS AC ) D CROSS APPLY ( -- Start date SELECT CAST('2014-01-01' AS date) AS StartDate ) BOT ) D;
No comments:
Post a Comment