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.
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.