Saturday, July 12, 2008

SQL Server Function to Determine a Leap Year

Problem
I need to write a function to determine if particular year is a leap year (i.e. February contains 29 days rather than 28 days). I know that there are various rules for calculating leap years. Is there an easy way to figure this out? Can you provide an example or two to validate various years?

Solution
Yes, there a few rules to consider when determining when a year is a leap year. For instance, contrary to popular belief not all years divisible by 4 are leap years. For instance, the year 1900 was not a leap year. However, you needn't bother yourself about leap year rules... you can let the SQL Server engine do the work for you!

The following scalar function takes in a year and returns a bit flag indicating whether the passed in year is a leap year or not.


create function dbo.fn_IsLeapYear (@year int)
returns bit
as
begin
return(select case datepart(mm, dateadd(dd, 1, cast((cast(@year as varchar(4)) + '0228') as datetime)))
when 2 then 1
else 0
end)
end
go

That's all there is to it! The function takes in the year, appends '0228' to it (for February 28th) and adds a day. If the month of the next day is a 2 (as extracted by the DATEPART function), then we're still in February so it must be a leap year! If not, it is not a leap year.

Here are a few examples:

select dbo.fn_IsLeapYear(1900) as 'IsLeapYear?'
select dbo.fn_IsLeapYear(2000) as 'IsLeapYear?'
select dbo.fn_IsLeapYear(2007) as 'IsLeapYear?'
select dbo.fn_IsLeapYear(2008) as 'IsLeapYear?'

As you can see, sometimes you can leverage the SQL Server engine to do some heavy lifting for you!

Saturday, July 05, 2008

Sony Computer Entertainment - Playstation 9

Super Cool Ads by Sony Computer Entertainment - Playstation 9 A superb cool advertisement from Sony Computer Entertainment for their Playstation 9 (PS9), which the company claims will be released in year 2078. Watch the video here see what the future holds >>

Cool Stuff: Sony Playstation 9

Friday, July 04, 2008

Weekly Quote - Freedom, Entrepreneurism & Enthusiasm


Quotes, Motivation, Personal Development,Freedom, Entrepreneurism, Enthusiasm, John Adams, Mark Victor, Mark Twain, Weekly Quote, Quotable Quote, Famous Word FREEDOM
“We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights that among these are Life, Liberty and the pursuit of Happiness.” ~The Declaration of Independence
“In order to live free and happily you must sacrifice boredom. It is not always an easy sacrifice.” ~Richard Bach
Posterity! You will never know how much it cost the present generation to preserve your freedom! I hope you will make good use of it! (letter to Abigail Adams, April 26, 1777) ~John Adams
“These are the times that try men’s souls. The summer soldier and the sunshine patriot will, in this crisis, shrink from the service of their country; but he that stands it now, deserves the love and thanks of man and woman.” ~Thomas Paine
Read more ..

Quotes of the Week - Freedom, Entrepreneurism & Enthusiasm

Thursday, July 03, 2008

DotA: WoWt DotA All-stars v6.52d Map

DotA: WOW DotA All-Stars v6.52d Map Free Download Link

World of Warcraft DotA v6.52d Map Download Link, Warcraft,   WOW,   Gaming, Download , Dota-Allstars, Spirit Bear, v6.49b ,   v6.43b,   AI Maps, 6.52c Map, Download Check out the Latest World Of Wwarcraft Dota-Allstars map is out.. here’s the change logs and download links of DotA v6.52d map at the end of the post .. Enjoy the maps download here >>

DotA: World of Warcraft DotA Allstars v6.52d Map Download Link

8 Ways to Detox and Cleanse Your Liver Naturally

Your liver is a remarkable organ that plays a vital role in keeping your body healthy. It's responsible for metabolizing nutrients, filt...