Saturday, December 8, 2012

How to Find Leap Year in sql server 2012

How to Find Leap Year in sql server 2012



How to determine whether a year is a leap year

To determine whether a year is a leap year, follow these steps:
  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (it has 366 days).
  5. The year is not a leap year (it has 365 days).

Here is the code for Finding LEAP YEAR in SQL SERVER



declare @chkyear int = 2011;

if @chkyear % 4 = 0
begin
  if @chkyear % 100 =0
   begin
         if @chkyear % 400 = 0
          Print CAST(@chkyear as varchar(4)) + N' is a Leap year';
   end
   else Print CAST(@chkyear as varchar(4)) + N' is a Leap year';
end
else print CAST(@chkyear as varchar(4)) + N' is not a Leap year';
 
Tags:Determine leap year , check for Leap year,Detecting a Leap Year in sql server 2012,Function to determine a leap year.

No comments:

Post a Comment