Created
November 30, 2015 22:45
-
-
Save jbnv/67cc446377756738d737 to your computer and use it in GitHub Desktop.
SQL Server [Calendar] view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍