WITH c
AS (
SELECT
Num = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1
FROM sys.columns c
CROSS JOIN sys.columns c1
)
, d
AS (
SELECT
[date] = DATEADD(day, Num, @startdate)
,Num
FROM c
WHERE
Num >= 0
AND Num <= DATEDIFF(day, @startdate, @enddate)
)
SELECT datekey = CAST(CONVERT(VARCHAR(8), DATEADD(day, Num, @startdate), 112) AS INT)
, [date]
, [DayOfMonth] = DATEPART(day, [Date])
, [DayName] = DATENAME(weekday, [Date])
, [DayOfYear] = DATEPART(dayofyear, [Date])
, [WeekOfYear] = DATEPART(week, [Date])
, [MonthName] = DATENAME(month, [Date])
, [MonthNumber] = DATEPART(month, [Date])
, [QuarterNumber] = DATEPART(quarter, [Date])
, [Year] = YEAR([date])
, [FiscalYear] = CASE WHEN DATEPART(month, [Date]) < 7 THEN YEAR([date]) ELSE YEAR([date]) + 1 END
FROM d
No comments:
Post a Comment