Following code sample is a sql function that returns if a given year is a leap year or not.
For example, if the input date is a date belonging to 2008, the bit returned is 1.
Similarly, if the date is one belonging to 2007, the bit returned is 0.
The logic for a leap year is that the year is divisible by 4 and not by 100. But if it divisible by 400 then, it is a leap year.
For example, 2000 is a leap year because it is divisible by 400. But 2100 is not because it is divisible by 100 and not by 400.
CREATE FUNCTION [dbo].[fn_IsLeapYear] ( @pDate DATETIME ) RETURNS BIT AS BEGIN
IF (YEAR( @pDate ) % 4 = 0 AND YEAR( @pDate ) % 100 != 0) OR YEAR( @pDate ) % 400 = 0 RETURN 1
RETURN 0
END GO
|
No responses found. Be the first to respond and make money from revenue sharing program.
|