產生民國年的 Time Table

CREATE TABLE DimTime
(
TimePK INT PRIMARY KEY,
[Year] SMALLINT NOT NULL,
[Quarter] TINYINT NOT NULL,
[Month] TINYINT NOT NULL,
[Day] TINYINT NOT NULL,
[ChineseMonthName] NVARCHAR(3) NOT NULL,
[ChineseWeekDay] NVARCHAR(3) NOT NULL
)

SET NOCOUNT ON
SET LANGUAGE 繁體中文
DECLARE @i INT=DATEDIFF(D,’1999/1/1′,’2020/12/31′),@j INT=0
WHILE @j<=@i
BEGIN
INSERT DimTime
VALUES(
CONVERT(INT,
CONVERT(VARCHAR(3),YEAR(DATEADD(d,@j,'1999/1/1'))-1911) +
RIGHT('0'+CONVERT(VARCHAR(2),Month(DATEADD(d,@j,'1999/1/1'))),2)+
RIGHT('0'+CONVERT(VARCHAR(2),Day(DATEADD(d,@j,'1999/1/1'))),2)),
YEAR(DATEADD(d,@j,'1999/1/1'))-1911,
DATEPART(Q,DATEADD(d,@j,'1999/1/1')),
Month(DATEADD(d,@j,'1999/1/1')),
DAY(DATEADD(d,@j,'1999/1/1')),
DATENAME(Month,DATEADD(d,@j,'1999/1/1')),
DATENAME(WEEKDAY,DATEADD(d,@j,'1999/1/1'))
)
SET @j+=1
END

SELECT * FROM DimTime
DROP TABLE DimTime

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: