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.
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.
As you can see, the leap year is calculated.
Good luck to everyone in business and life.