Leap Year

A leap year can be calculated using a formula (Please see Microsoft article), however there is no real need to calculate it this way. All that is needed in sql is to take one day away from March 1st; with the resultant being either the 28th, or in the case of a leap year the 29th. Finally by substracting 28 this is simplified to 1 or 0.

Method to determine whether a year is a leap year
http://support.microsoft.com/kb/214019

--http://jongurgul.com/blog/leap-year/
DECLARE @Leap TABLE ([yyyy] INT)
INSERT INTO @Leap VALUES (YEAR(GETDATE()))
INSERT INTO @Leap VALUES (YEAR(DATEADD(yyyy,1,GETDATE())))
INSERT INTO @Leap VALUES (YEAR(DATEADD(yyyy,2,GETDATE())))
INSERT INTO @Leap VALUES (YEAR(DATEADD(yyyy,3,GETDATE())))
SELECT 
 [yyyy]
,DATEPART(d,DATEADD(d,-1,CAST(CAST([yyyy]*10000+0301 AS CHAR(8)) AS DATETIME)))-28 [isLeap] 
FROM @Leap

Leave a Reply