Author |
Topic |
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-09-25 : 16:16:22
|
while was reading one of the old posts here, I came across the code below. I could not understand the use of '%' in this code.link : http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=83637declare @d datetimeset @d='2100-01-01'Select case when year(@d)%400=0 then 'yes' when year(@d)%100=0 then 'no' when year(@d)%4=0 then 'yes' else 'no' end as is_leap_year link I thought it was divide but dividing by 400 would not get 0 so I'm confused. |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-09-25 : 16:20:13
|
modulo Too old to Rock'n'Roll too young to die. |
|
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-09-26 : 03:53:44
|
Thanks WebfredI just read through more about modulo in Google and it seems modulo is just an another word for remainder.But in the above example suppose date was 2005 then 2005 DIVIDED BY 100 comes 5 as remainder, not 0. Then how it works? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-09-26 : 04:55:20
|
quote: Originally posted by learning_grsql Thanks WebfredI just read through more about modulo in Google and it seems modulo is just an another word for remainder.But in the above example suppose date was 2005 then 2005 DIVIDED BY 100 comes 5 as remainder, not 0. Then how it works?
2005 devided by 100If the remainder isn't zero then it is no leap year.Where are your problems in understandig:- the use of modulo or- the rules to say if a year is a leap year? Too old to Rock'n'Roll too young to die. |
|
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-09-26 : 12:40:44
|
I think the below statement I cannot understandwhen year % 100 = 0 Then no leap year As it stands, I understand when a year is divided by 100 and the remainder is zero, then it is not a leap year.If I just assume, the year is 2005 then 2005 % 100 comes remainder 5 then as per condition it is a leap year but in fact it is not when I run the code. So I'm confused. |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2012-09-26 : 14:07:58
|
You seem to focus only on part of the case statement, and not the whole statement.Evaluating the code with the year 2005:when year(@d)%400=0 <--- this is not true, so we continue evaluatingwhen year(@d)%100=0 <--- this is not true, so we continue evaluatingwhen year(@d)%4 <--- this is not true, so we continue evaluatingelse <--- this is true, so we return "no" |
|
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-09-26 : 16:05:59
|
Thank you very much bitsmed and Webfred. Now I have got it. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2012-10-06 : 02:54:17
|
Here is a simple method. Find the day of year for the last day of the year and subtract 365 to give 0 for non-leap year and 1 for leap year.select a.[Year], IsLeapYear = datepart(dy,dateadd(yy,a.[Year]-1899,-1))-365from ( -- Test Data select [Year] = 1900 union all select [Year] = 1999 union all select [Year] = 2000 union all select [Year] = 2001 union all select [Year] = 2002 union all select [Year] = 2003 union all select [Year] = 2004 ) a Results:Year IsLeapYear----------- -----------1900 01999 02000 12001 02002 02003 02004 1 CODO ERGO SUM |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-10-16 : 09:47:30
|
Thats good one MVJ MadhivananFailing to plan is Planning to fail |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2012-10-16 : 10:39:47
|
quote: Originally posted by madhivanan Thats good one MVJ MadhivananFailing to plan is Planning to fail
Feel free to add it to your blog post. CODO ERGO SUM |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-10-16 : 10:52:34
|
quote: Originally posted by Michael Valentine Jones
quote: Originally posted by madhivanan Thats good one MVJ MadhivananFailing to plan is Planning to fail
Feel free to add it to your blog post. CODO ERGO SUM
Done. Thanks MadhivananFailing to plan is Planning to fail |
|
|
|