Function to Find Leap Year Using EOMONTH Function in SQL Server
Hello to everyone,
In this article I will talk about how to find leap years in SQL Server.
You can read my previous article on the subject at the link below.
According to the comment made by Barak G. on the Linkedin platform, I revised the leap year check function.
You can easily do this with the help of the function below.
SELECT dbo.FindLeapYearEOMONTH('2021.01.01') AS ResultCREATE FUNCTION FindLeapYearEOMONTH ( @Date DATE ) RETURNS VARCHAR(30) BEGIN DECLARE @Result VARCHAR(30); IF DAY(EOMONTH(@Date, 2 - MONTH(@Date))) = 29 BEGIN SET @Result = 'The year is leap year'; END; ELSE BEGIN SET @Result = 'The year is not leap year'; END; RETURN @Result END; --Use of Function SELECT dbo.FindLeapYearEOMONTH('2021.01.01') AS Result
When you run the code you will get the following result.
As you can see, the leap year is calculated.
Good luck to everyone in business and life.