Skip to content

Instantly share code, notes, and snippets.

@dgreenway
Last active August 24, 2021 20:19
Show Gist options
  • Save dgreenway/2dccceb1389d94d547595237d606b005 to your computer and use it in GitHub Desktop.
Save dgreenway/2dccceb1389d94d547595237d606b005 to your computer and use it in GitHub Desktop.
SQL Server Calendar Table #SQL #mssql
DECLARE @StartDate date = '20100101';
DECLARE @CutoffDate date = DATEADD(DAY, -1, DATEADD(YEAR, 30, @StartDate));
;WITH seq(n) AS
(
SELECT 0 UNION ALL SELECT n + 1 FROM seq
WHERE n < DATEDIFF(DAY, @StartDate, @CutoffDate)
),
d(d) AS
(
SELECT DATEADD(DAY, n, @StartDate) FROM seq
),
src AS
(
SELECT
TheDate = CONVERT(date, d),
TheDay = DATEPART(DAY, d),
TheDayName = DATENAME(WEEKDAY, d),
TheWeek = DATEPART(WEEK, d),
TheISOWeek = DATEPART(ISO_WEEK, d),
TheDayOfWeek = DATEPART(WEEKDAY, d),
TheMonth = DATEPART(MONTH, d),
TheMonthName = DATENAME(MONTH, d),
TheQuarter = DATEPART(Quarter, d),
TheYear = DATEPART(YEAR, d),
TheFirstOfMonth = DATEFROMPARTS(YEAR(d), MONTH(d), 1),
TheLastOfYear = DATEFROMPARTS(YEAR(d), 12, 31),
TheDayOfYear = DATEPART(DAYOFYEAR, d)
FROM d
)
SELECT * FROM src
ORDER BY TheDate
OPTION (MAXRECURSION 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment