Date functions – Leap Date

Simple function to check, if year is a leap year or not.

/***********************************************
Leap year function. 
It checks, if any given year is a leap year or not.
-------------------
Example:
select dbo.IsLeapYear(2013) - should return 0
select dbo.IsLeapYear(2016) - should return 1
***********************************************/

CREATE FUNCTION [dbo].[IsLeapYear] ( @Year INT )
RETURNS BIT
AS 
    BEGIN

        DECLARE @Date DATETIME ,@YearString VARCHAR(4)
        SET @YearString = CAST(@Year AS VARCHAR)
                    
/*	calculate March 1st	*/
        SET @Date = CONVERT(DATETIME, '03/01/' + @YearString, 101)

/*	calculate the date of  last day in Fabruary	*/
        SET @Date = DATEADD(d, -1, @Date)

        IF ( DAY(@Date) = 29 )  RETURN 1        
        RETURN 0

    END

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s