Pages

Social Icons

Thursday 1 March 2012

How to check given year is leap year or not

There is a function which gives wheather given year is leapyear or not



CREATE FUNCTION dbo.IsLeapYear (@year INT)
RETURNS INT
AS
BEGIN
DECLARE @value INT
DECLARE @datetime DATETIME

SET @datetime = CONVERT(DATETIME,'02/01/'+CONVERT(VARCHAR,@year),101)

IF (DATEPART(DD,DATEADD(DD,-1,DATEADD(MM,1,CAST(CAST(YEAR(@datetime) AS VARCHAR)+'-'+CAST(MONTH(@datetime) AS VARCHAR)+'-01' AS DATETIME)))) = 29 )
BEGIN SET @value = 1 END ELSE BEGIN SET @value = 0 END 

RETURN @value
END


we can use this function to check the year if the given year is leap year then It will return 1 Otherwise it will return 0.



No comments:

Post a Comment