Function to Find Leap Year in SQL Server

Hello to everyone,

In this article I will talk about how to find leap years in SQL Server.

Leap years are years where an extra, or intercalary, day is added to the end of the shortest month, February. The intercalary day, February 29, is commonly referred to as leap day.

Function to Find Leap Year in SQL Server

You can easily do this with the help of the function below.

CREATE FUNCTION fn_LeapYearCheck
(
    @Date DATETIME
)
RETURNS VARCHAR(50)
AS
BEGIN
    DECLARE @ReturnString VARCHAR(50);
    SET @ReturnString = CASE
                            WHEN (
                                     YEAR(@Date) % 4 = 0
                                     AND YEAR(@Date) % 100 != 0
                                 )
                                 OR YEAR(@Date) % 400 = 0 THEN
                                'The year is a leap year'
                            ELSE
                                'The year is not a leap year'
                        END;
    RETURN @ReturnString;
END;
GO


--Use of Function

SELECT dbo.fn_LeapYearCheck('2020-01-01')

When you run the code you will get the following result.

Function to Find Leap Year in SQL Server

As you can see, the leap year is calculated.

Good luck to everyone in business and life.

537 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!