Total days in a Year

Pinal Dave, Blogger and Speaker, posted about “SQL SERVER – Trivia – Days in a Year“. There are two methods shown in the post to find out total number of days for a given year. Here is one more method

DECLARE @year AS INT
SET @year=2012
select 365+
case
when @year%400=0 then 1
when @year%100=0 then 0
when @year%4=0 then 1
else 0
end

The result is 366.

The logic is that every year has 365 days. We need to find if it is a Leap year. If it is add 1 to it

Advertisements

2 thoughts on “Total days in a Year

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s