Here's a simple script that can detect if the current year is a LEAP year. You can create a function out of this query and use it in your projects to detect if the year is a leap year and take decisions based on the same.
-- Detect if the year is a LEAP year
DECLARE @dt datetime
DECLARE @yr int
DECLARE @y char(4)
SET @dt = GETDATE()
SET @yr = YEAR(@dt)
SET @y = CAST(@yr as char(4))
SELECT CASE
-- Years like 2000, 2400 are always leap years
WHEN @yr % 400=0 then @y + ' is a Leap Year'
-- Years like 1700, 1900 divisible by 100
-- but not divisible by 400, are not leap years
WHEN @yr % 100=0 then @y + ' is not a Leap Yr'
-- Years like 2004 that are not divisible by
-- 400 and 100 but are divisible by 4, are leap years
WHEN @yr % 4=0 then @y + ' is a Leap Year'
ELSE @y + ' is not a Leap Yr'
END
AS LeapYearDetection
About The Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and a new one recently at
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.
Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook
1 comment:
there's an easier way to do this by letting sql server do the work for you
if datepart(dd, cast(cast(datepart(yy, getdate()) as varchar) + '-02-28' as datetime) + 1) = 29
print 'leap year'
else
print 'not a leap year'
Post a Comment