0 Replies - 1517 Views - Last Post: 28 September 2008 - 03:51 PM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Determine is a date is a leap year

Posted 28 September 2008 - 03:51 PM

Description: This is a User Defined function to determine if a given year is a leap year
CREATE FUNCTION dbo.udfIsLeapYear (@Date DATETIME)
RETURNS INT
AS
BEGIN
	/*
	To determine is a year is a Leap year the year must:
		1) Be divisible by 4 and not divisible by 100
		2) Be divisible by 400
	*/
    IF (YEAR(@Date) % 4 = 0 AND YEAR(@Date) % 100 != 0) OR
        YEAR(@Date) % 400 = 0
        RETURN 1
   RETURN 0
END
GO

/*
	Example Usage:

	This will get the number of days in a provided year. It will
	check the date and see if it's a leap year and add either 1 to
	365 (leap year has 366 days) or 0
*/
DECLARE @TotalDaysInYear INT
SET @TotalDaysInYear = 365 + dbo.udfIsLeapYear(GETDATE())



Is This A Good Question/Topic? 0
  • +

Page 1 of 1