Skip to content

Instantly share code, notes, and snippets.

@jbnv
Created November 30, 2015 22:45
Show Gist options
  • Save jbnv/67cc446377756738d737 to your computer and use it in GitHub Desktop.
Save jbnv/67cc446377756738d737 to your computer and use it in GitHub Desktop.
SQL Server [Calendar] view.
/*
A view that creates a view of date information, in a range from 2000 to 2179.
Use this view to perform date amd time operations would otherwise be difficult with standard T-SQL methods.
*/
CREATE VIEW [dbo].[Calendar]
AS
SELECT [Date]
,DATEPART(year,[Date]) AS [Year]
,DATEPART(month,[Date]) AS [Month]
,DATEPART(day,[Date]) AS [Day]
FROM (
SELECT
(a.Number * 256) + b.Number AS N
FROM
(
SELECT number
FROM master..spt_values
WHERE type = 'P' AND number <= 255
) a (Number),
(
SELECT number
FROM master..spt_values
WHERE type = 'P' AND number <= 255
) b (Number)
) numbers
CROSS APPLY (SELECT DATEADD(day,N,'2000-1-1') AS [Date]) d
@shamlance
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment