How to check a year is leap year or not using SQL function

Description :


Create a function to check leap year


create FUNCTION CHECK_LEAP_YEAR
(@year SMALLINT)
RETURNS BIT
AS
BEGIN
DECLARE @leapdate SMALLDATETIME
DECLARE @check_day TINYINT

SET @leapdate = CONVERT(VARCHAR(4), @year) + '0228'
SET @check_day = DATEPART(d, DATEADD(d, 1, @leapdate))
IF (@check_day = 29)
RETURN 1

RETURN 0
END


SELECT dbo.CHECK_LEAP_YEAR(2000) as checkLeapYear
run abopve query

pass the year, if it is leap year the result will be returned as 1 if not then 0.


Comments

Author: Abhay Kumar Raut29 Aug 2008 Member Level: Gold   Points : 2

if it will review by Editors

Author: Mohan16 Nov 2009 Member Level: Gold   Points : 1

This also a one type of method to find Leap Year



select case (YEAR(getdate()))%4 when 0 then 'Leap Year' else 'Not Leap Year' end as Leap



Author: Abhay Kumar Raut16 Nov 2009 Member Level: Gold   Points : 1

Hi Mohan ,

Your query is wrong.
Every year which is divisible by 4 isn't a leap year.
This is logic

if(yr%400 == 0) || ((yr%4 == 0) && (yr%100 != 0))

So i have written some logic in that function.

Thanks,
Abhay

Author: Mohan16 Nov 2009 Member Level: Gold   Points : 0

Yes i accept . Thanks for your information..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: