Today is a special day which comes only after four years -29th Feb. So I thought I should write something about leap year.
First, what is leap year?
- The year must be divisible by 4 and must NOT be divisible by 100.
- The year must be divisible by 400.
In some scenario, you need to count the number of days in a year Like Banking application where in savings account interest is calculated based on number of days in year. A year can have 366 days or 365 days based on whether year is Leap or not.
I am writing one user defined function which takes data as parameter and returns 1 if it is leap year and 0 if it is not.
CREATE FUNCTION [dbo].[udf_IsLeapYear] ( @Date DATETIME )
RETURNS BIT
AS
BEGIN
IF (YEAR( @Date ) % 4 = 0 AND YEAR( @Date ) % 100 != 0) OR
YEAR( @Date ) % 400 = 0
RETURN 1
RETURN 0
END
GO
Example: How to calculate number of days in year.
DECLARE @DaysInYear INT
SET @DaysInYear = 365 + [dbo].[ufn_IsLeapYear] ( GETDATE() )
print @DaysInYear
It will print 366.
Determine Leap Year
Reviewed by kamal kumar das
on
February 29, 2012
Rating:
No comments: