SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Checking for Leap Year
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rauof_thameem
Starting Member

31 Posts

Posted - 05/15/2007 :  23:51:59  Show Profile  Reply with Quote
Hi..,

How do i can check for leap year in date

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 05/16/2007 :  00:01:28  Show Profile  Reply with Quote
what is the input like ? just the year ?


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 05/16/2007 :  00:02:10  Show Profile  Reply with Quote
Wiki has a good explanation on this http://en.wikipedia.org/wiki/Leap_year


KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 05/16/2007 :  00:27:06  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote

declare @d datetime
set @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


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rauof_thameem
Starting Member

31 Posts

Posted - 05/16/2007 :  00:46:07  Show Profile  Reply with Quote
Thanks Mahadi
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 05/16/2007 :  00:56:48  Show Profile  Reply with Quote

select
	YEAR,
	IS_LEAP_YEAR =
	-- If 60th day of the year is in February, then it's leap year.
	case month(dateadd(yy,a.YEAR-1900,0)+59) when 2 then 'yes' else 'no' end
from
	(
	select YEAR = 1800	union all
	select YEAR = 1900	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	union all
	select YEAR = 2005	union all
	select YEAR = 2006	union all
	select YEAR = 2007	union all
	select YEAR = 2008	union all
	select YEAR = 2009	union all
	select YEAR = 2010	union all
	select YEAR = 2011	union all
	select YEAR = 2400
	) a


Results:

YEAR        IS_LEAP_YEAR 
----------- ------------ 
1800        no
1900        no
2000        yes
2001        no
2002        no
2003        no
2004        yes
2005        no
2006        no
2007        no
2008        yes
2009        no
2010        no
2011        no
2400        yes

(15 row(s) affected)



CODO ERGO SUM

Edited by - Michael Valentine Jones on 05/16/2007 01:00:42
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 05/16/2007 :  10:58:17  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Good Logic. I knew you will come up with different logic

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 05/16/2007 :  11:03:42  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by rauof_thameem

Thanks Mahadi

Check my name carefully

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

anki0121
Starting Member

India
1 Posts

Posted - 04/03/2009 :  04:06:48  Show Profile  Reply with Quote
This is very simple....
select (case to_number(to_char((trunc(to_date('15/08/08','dd/mm/yy'),'yy')+59),'mm')) when 2 then 'Yes' else 'No' end) as yr
from dual

here I have first found the first day of the year
which can be done by using
Select Trunc(To_date(SYSDATE),'yy')
from dual


and then adding 59 days to it and then using the case statement to check whether the month is 2 or 3 depending on which leap year can be found out

When u need someone...
Ankeyzzzz always dere
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 04/03/2009 :  05:08:58  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by anki0121

This is very simple....
select (case to_number(to_char((trunc(to_date('15/08/08','dd/mm/yy'),'yy')+59),'mm')) when 2 then 'Yes' else 'No' end) as yr
from dual

here I have first found the first day of the year
which can be done by using
Select Trunc(To_date(SYSDATE),'yy')
from dual


and then adding 59 days to it and then using the case statement to check whether the month is 2 or 3 depending on which leap year can be found out

When u need someone...
Ankeyzzzz always dere


Are you aware that this site is specific to MS SQL Server?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000