Если я правильно понял, вы хотите заполнить таблицу DimDateAlbum
.Я немного отредактировал вашу таблицу (добавил ограничение идентичности, чтобы не писать это поле вручную), и теперь он выглядит так:
CREATE TABLE [DimDateAlbum]
(
[DateKey] INT IDENTITY CONSTRAINT PK_DimDateAlbum_ID PRIMARY KEY,
[Date] DATETIME NOT NULL,
[Year] INT NOT NULL,
[Quarter] TINYINT NOT NULL,
[QuarterName] VARCHAR(50) NOT NULL, -- January to March: First, April to
[Month] TINYINT NOT NULL,
[MonthName] VARCHAR(9) NOT NULL, -- January, February etc
[Day] TINYINT NOT NULL, -- Field holds day number of Month
[DayofWeek] TINYINT NOT NULL,
[WeekName] VARCHAR(50) NOT NULL, -- Field displays 1: Monday, 2: Tuesday etc
)
И теперь вы можете вставлять свои данные.Я добавил тестовую переменную для вставки одной строки, однако ее можно использовать для вставки из таблицы:
INSERT INTO dbo.DimDateAlbum
(
DateKey,
Date,
Year,
Quarter,
QuarterName,
Month,
MonthName,
Day,
DayofWeek,
WeekName
)
SELECT
CAST(a.Release_Date AS DATETIME)
, YEAR(CAST(a.Release_Date AS DATETIME)) --
, DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) -- Quarter
, CASE -- Quarter Name
WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 1 THEN 'January to March' -- Quarter Name
WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 2 THEN 'April to June' -- Quarter Name
WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 3 THEN 'July to September' -- Quarter Name
WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 4 THEN 'October to December' -- Quarter Name
END
, MONTH(CAST(a.Release_Date AS DATETIME)) -- Month number
, DATENAME(MONTH, DATEADD( MONTH, MONTH(CAST(a.Release_Date AS DATETIME)), 0) - 1) -- Month name
, DAY(CAST(a.Release_Date AS DATETIME)) -- 6
, DATEPART(dw, CAST(a.Release_Date AS DATETIME)) -- 5
, DATENAME(dw, CAST(a.Release_Date AS DATETIME)) -- Thursday
FROM Album a
Пример работы:
DECLARE @FooDate VARCHAR(30) = '2018-12-06 12:10:51.727'
INSERT INTO dbo.DimDateAlbum
(
DateKey,
Date,
Year,
Quarter,
QuarterName,
Month,
MonthName,
Day,
DayofWeek,
WeekName
)
SELECT
CAST(@FooDate AS DATETIME)
, YEAR(CAST(@FooDate AS DATETIME)) --
, DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) -- Quarter
, CASE -- Quarter Name
WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 1 THEN 'January to March' -- Quarter Name
WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 2 THEN 'April to June' -- Quarter Name
WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 3 THEN 'July to September' -- Quarter Name
WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 4 THEN 'October to December' -- Quarter Name
END
, MONTH(CAST(@FooDate AS DATETIME)) -- Month number
, DATENAME(MONTH, DATEADD( MONTH, MONTH(CAST(@FooDate AS DATETIME)), 0) - 1) -- Month name
, DAY(CAST(@FooDate AS DATETIME)) -- 6
, DATEPART(dw, CAST(@FooDate AS DATETIME)) -- 5
, DATENAME(dw, CAST(@FooDate AS DATETIME)) -- Thursday