SqlServer创建时间维度
Posted 刀子道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SqlServer创建时间维度相关的知识,希望对你有一定的参考价值。
DECLARE @BeginDate DATE; SELECT @BeginDate = ‘2018-1-1‘; WHILE @BeginDate <= ‘2021-12-31‘ BEGIN INSERT INTO Dim_Date SELECT Cast(CONVERT(VARCHAR(10), @BeginDate, 112) AS INT) AS DateKey, Year(@BeginDate) AS Year, Datepart(QUARTER, @BeginDate) AS Quarter, CASE WHEN Datepart(QUARTER, @BeginDate) = 1 THEN ‘第一季度‘ WHEN Datepart(QUARTER, @BeginDate) = 2 THEN ‘第二季度‘ WHEN Datepart(QUARTER, @BeginDate) = 3 THEN ‘第三季度‘ ELSE ‘第四季度‘ END AS QuarterCN, Month(@BeginDate) AS Month, CASE WHEN Month(@BeginDate) = 1 THEN ‘一月‘ WHEN Month(@BeginDate) = 2 THEN ‘二月‘ WHEN Month(@BeginDate) = 3 THEN ‘三月‘ WHEN Month(@BeginDate) = 4 THEN ‘四月‘ WHEN Month(@BeginDate) = 5 THEN ‘五月‘ WHEN Month(@BeginDate) = 6 THEN ‘六月‘ WHEN Month(@BeginDate) = 7 THEN ‘七月‘ WHEN Month(@BeginDate) = 8 THEN ‘八月‘ WHEN Month(@BeginDate) = 9 THEN ‘九月‘ WHEN Month(@BeginDate) = 10 THEN ‘10月‘ WHEN Month(@BeginDate) = 11 THEN ‘11月‘ ELSE ‘12月‘ END AS MonthCN, CASE WHEN Datepart(DAY, @BeginDate) <= 10 THEN 1 WHEN Datepart(DAY, @BeginDate) > 20 THEN 3 ELSE 2 END AS Ten, CASE WHEN Datepart(DAY, @BeginDate) <= 10 THEN ‘上旬‘ WHEN Datepart(DAY, @BeginDate) > 20 THEN ‘下旬‘ ELSE ‘中旬‘ END AS TenCN, Datepart(WEEK, @BeginDate) AS Week, DATENAME(WeekDay, @BeginDate) AS WeekDay, Day(@BeginDate) AS Day, CONVERT(VARCHAR(10),@BeginDate,120) AS Date SET @BeginDate = DATEADD(DAY,1,@BeginDate); END;
以上是关于SqlServer创建时间维度的主要内容,如果未能解决你的问题,请参考以下文章
具有运行时 pojos 的带有 Hibernate 的 OSGi 片段包
使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段