Checking LEAP YEAR through SQL Server.

vishalneeraj-24503
Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 373
As we know that,Leap year comes once in every four year.And the funda behind LEAP year is,the Year is fully divided with 4.That is the reason,we get 366 days in a Year.
Below is a sample script:-

CREATE FUNCTION [Is_Leap_Year] (@Input_Date DATETIME)
RETURNS Bit
AS
BEGIN
IF (YEAR(@Input_Date) % 4 = 0 AND YEAR(@Input_Date) % 100 != 0) OR YEAR(@Input_Date) % 400 = 0
RETURN 1;
RETURN 0;
END
GO


Now,to test above function:-
DECLARE @DaysInYear INT;
SET @DaysInYear = 365 + [dbo].[Is_Leap_Year] (DATEADD(YY,2,GETDATE())); -- 2016
PRINT @DaysInYear;

SET @DaysInYear = 365 + [dbo].[Is_Leap_Year] (DATEADD(YY,1,GETDATE())); --2015
PRINT @DaysInYear;

SET @DaysInYear = 365 + [dbo].[Is_Leap_Year] (GETDATE()); --2014
PRINT @DaysInYear;

--The year must be divisible by 4 and must NOT be divisible by 100.
--The year must be divisible by 400.


Output:-
366
365
365

2016 is fully divided by 4 then it's a LEAP YEAR.

Comments or Responses

Login to post response