Leap Year Checking Function in SQL Server

Hello everybody,

In this article, I will try to give information about the leap year control function in SQL Server.

In SQL Server, in some cases, you may want to check for leap years.

You can easily do this using the function below.

CREATE FUNCTION dbo.IsLeapYear
(
    @year INT
)
RETURNS BIT
AS
BEGIN
    DECLARE @d DATETIME,
            @ans BIT;
    SET @d = CONVERT(DATETIME, '31/01/' + CONVERT(VARCHAR(4), @year), 103);
    IF DATEPART(DAY, DATEADD(MONTH, 1, @d)) = 29
        SET @ans = 1;
    ELSE
        SET @ans = 0;
    RETURN @ans;
END;
GO

--Use of function

SELECT dbo.IsLeapYear(2016);

When you create and run the above function, you will see a result similar to the one below.

Leap Year Checking Function in SQL Server

As you can see, we have seen whether the entered year is a leap year or not.

Good luck to everyone in business and life.

14 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!