年月日星期 对照表

SQL Server - 练习 专栏收录该内容
48 篇文章 0 订阅
--年月日星期 对照表
IF OBJECT_ID('DimDateStartWithMonday') IS NOT NULL
    DROP TABLE DimDateStartWithMonday
GO
CREATE TABLE DimDateStartWithMonday
(
	DateKey               INT PRIMARY KEY,
	FullDate              DATE NOT NULL,
	[DateName]            NVARCHAR(20),
	DayNumberOfWeek       TINYINT NOT NULL,
	DayNameOfWeek         NVARCHAR(10) NOT NULL,
	DayNumberOfMonth      TINYINT NOT NULL,
	DayNumberOfYear       SMALLINT NOT NULL,
	WeekNumberOfYear      TINYINT NOT NULL,
	EnglishMonthName      NVARCHAR(10) NOT NULL,
	MonthNumberOfYear     TINYINT NOT NULL,
	CalendarQuarter       TINYINT NOT NULL,
	CalendarSemester      TINYINT NOT NULL,
	CalendarYear          SMALLINT NOT NULL
)
GO
--
SET DATEFIRST 1
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SELECT @StartDate = '2000-01-01',
       @EndDate = '2100-12-31'

WHILE (@StartDate < + @EndDate)
BEGIN
    INSERT INTO DimDateStartWithMonday
      (
        DateKey,
        FullDate,
        [DateName],
        DayNumberOfWeek,
        DayNameOfWeek,
        DayNumberOfMonth,
        DayNumberOfYear,
        WeekNumberOfYear,
        EnglishMonthName,
        MonthNumberOfYear,
        CalendarQuarter,
        CalendarSemester,
        CalendarYear
      )
    SELECT CAST(CONVERT(VARCHAR(8), @StartDate, 112) AS INT) AS DateKey,
           CONVERT(VARCHAR(10), @StartDate, 20) AS FullDate,
           CONVERT(VARCHAR(20), @StartDate, 106) AS [DateName],
           DATEPART(DW, @StartDate)  AS DayNumberOfWeek,
           DATENAME(DW, @StartDate)  AS DayNameOfWeek,
           DATENAME(DD, @StartDate)  AS [DayOfMonth],
           DATENAME(DY, @StartDate)  AS [DayOfYear],
           DATEPART(WW, @StartDate)  AS WeekNumberOfYear,
           DATENAME(MM, @StartDate)  AS EnglishMonthName,
           DATEPART(MM, @StartDate)  AS MonthNumberOfYear,
           DATEPART(QQ, @StartDate)  AS CalendarQuarter,
           CASE 
                WHEN DATEPART(MM, @StartDate) BETWEEN 1 AND 6 THEN 1
                ELSE 2
           END                       AS CalendarSemester,
           DATEPART(YY, @StartDate)  AS CalendarYear
    
    SET @StartDate = @StartDate + 1
END
GO

--
SELECT TOP 365 * FROM DimDateStartWithMonday

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值