Dim_日期:
CREATE TABLE [dbo].[Dim_日期]( [日期ID] [varchar](8) NOT NULL, [年] [int] NULL, [半年] [varchar](6) NULL, [季] [varchar](2) NULL, [月] [varchar](4) NULL, [周] [varchar](6) NULL, [星期] [varchar](6) NULL, [是否周末] [varchar](4) NULL, [日] [int] NULL, CONSTRAINT [PK_Dim_日期] PRIMARY KEY CLUSTERED ( [日期ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
生成数据:
SET DATEFIRST 1 --设置周一每周第一天,默认为7 DECLARE @BeginDate DATE; SELECT @BeginDate = '2000-1-2'; WHILE @BeginDate <= '2016-12-31' BEGIN INSERT INTO [Dim_日期] SELECT CONVERT(VARCHAR(8), @BeginDate, 112) AS [日期ID], Year(@BeginDate) AS [年], CASE when DATEPART(month, @BeginDate)<=6 then '上半年' else '下半年' end as [半年], CASE WHEN Datename (qq, @BeginDate) = '1' THEN 'Q1' WHEN Datename (qq, @BeginDate) = '2' THEN 'Q2' WHEN Datename (qq, @BeginDate) = '3' THEN 'Q3' ELSE 'Q4' END AS [季], CASE WHEN Month(@BeginDate) = 1 THEN '01月' WHEN Month(@BeginDate) = 2 THEN '02月' WHEN Month(@BeginDate) = 3 THEN '03月' WHEN Month(@BeginDate) = 4 THEN '04月' WHEN Month(@BeginDate) = 5 THEN '05月' WHEN Month(@BeginDate) = 6 THEN '06月' WHEN Month(@BeginDate) = 7 THEN '07月' WHEN Month(@BeginDate) = 8 THEN '08月' WHEN Month(@BeginDate) = 9 THEN '09月' WHEN Month(@BeginDate) = 10 THEN '10月' WHEN Month(@BeginDate) = 11 THEN '11月' ELSE '12月' END AS [月], CASE WHEN Datepart(WEEK, @BeginDate) < 10 THEN '第0' + CONVERT(VARCHAR(2), Datepart(WEEK, @BeginDate)) + '周' ELSE '第' + CONVERT(VARCHAR(2), Datepart(WEEK, @BeginDate)) + '周' END AS [周], Datename(WeekDay, @BeginDate) AS [星期], CASE WHEN Datepart(dw, @BeginDate) = '6' THEN '周末' WHEN Datepart(dw, @BeginDate) = '7' THEN '周末' ELSE '平时' END AS '是否周末', Day(@BeginDate) AS [日] SET @BeginDate = Dateadd(DAY, 1, @BeginDate); END;