How to check a year is leap year or not using SQL function

Create a function to check leap year

create FUNCTION CHECK_LEAP_YEAR

(@year SMALLINT)

RETURNS BIT

AS

BEGIN

    DECLARE @leapdate SMALLDATETIME

    DECLARE @check_day TINYINT

    SET @leapdate = CONVERT(VARCHAR(4), @year) + '0228'

    SET @check_day = DATEPART(d, DATEADD(d, 1, @leapdate))

    IF (@check_day = 29)

        RETURN 1

    RETURN 0

END

SELECT dbo.CHECK_LEAP_YEAR(2000) as checkLeapYear

Run above query and pass the year, if it is leap year the result will be returned as 1 if not then 0.

Comments