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
 General SQL Server Forums
 New to SQL Server Programming
 how to find year end and year start
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

qutesanju
Posting Yak Master

193 Posts

Posted - 02/05/2009 :  08:55:44  Show Profile  Reply with Quote
how to find year end and year start from date entered?

Jai Krishna
Constraint Violating Yak Guru

India
333 Posts

Posted - 02/05/2009 :  09:05:08  Show Profile  Reply with Quote
select dateadd(d,-datepart(dy,getdate())+1,getdate()) as yearstart,
dateadd(d,-datepart(d,getdate()),dateadd(m,13-datepart(m,getdate()),getdate())) as yearend


Jai Krishna
Go to Top of Page

Rajesh Jonnalagadda
Starting Member

India
45 Posts

Posted - 02/05/2009 :  09:37:32  Show Profile  Visit Rajesh Jonnalagadda's Homepage  Reply with Quote
Hi,
A small change in Jai krishna yearstart query,

SELECT DateAdd(d, -DatePart(dy,getdate()-1),getdate()) as YearStart,
DateAdd(d, -DatePart(d,getdate()), Dateadd(m,13-datepart(m,getdate()),getdate())) as YearEnd



Rajesh Jonnalagadda
GGK TECH
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

India
333 Posts

Posted - 02/05/2009 :  10:04:21  Show Profile  Reply with Quote
quote:
Originally posted by Rajesh Jonnalagadda

Hi,
A small change in Jai krishna yearstart query,

SELECT DateAdd(d, -DatePart(dy,getdate()-1),getdate()) as YearStart,
DateAdd(d, -DatePart(d,getdate()), Dateadd(m,13-datepart(m,getdate()),getdate())) as YearEnd



Rajesh Jonnalagadda
GGK TECH




I think change is not that necessary as my query is producing correct results

Jai Krishna
Go to Top of Page

jek2201
Starting Member

1 Posts

Posted - 05/26/2009 :  04:40:44  Show Profile  Reply with Quote
A simple one if you want also to have hours, minuttes and the seconds right as well:

--Year Start
select dateadd(year, (select year(getdate())) - 1900 , '01-01-1900')

--Year End (for some reason -1 wont work and -2 really gives minus 3 but you are into miliseconds)
select dateadd(ms, -2, (select dateadd(year, (select year(getdate())) - 1900 + 1 , '01-01-1900')))
Go to Top of Page

bklr
Flowing Fount of Yak Knowledge

India
1693 Posts

Posted - 05/26/2009 :  04:55:15  Show Profile  Reply with Quote
hai rajesh,
both queries will give u same output
select dateadd(d,-datepart(dy,getdate())+1,getdate()) as yearstart,
dateadd(d,-datepart(d,getdate()),dateadd(m,13-datepart(m,getdate()),getdate())) as yearend

SELECT DateAdd(d, -DatePart(dy,getdate()-1),getdate()) as YearStart,
DateAdd(d, -DatePart(d,getdate()), Dateadd(m,13-datepart(m,getdate()),getdate())) as YearEnd

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30242 Posts

Posted - 05/26/2009 :  05:11:50  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Keep it simple
SELECT	DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) AS YearStart,
	DATEADD(YEAR, DATEDIFF(YEAR, -1, GETDATE()), -1) AS YearEnd



E 12°55'05.63"
N 56°04'39.26"

Edited by - SwePeso on 05/26/2009 05:12:01
Go to Top of Page

HenryJin
Starting Member

Australia
1 Posts

Posted - 11/07/2012 :  20:59:49  Show Profile  Reply with Quote
SwePeso's code is working perfect for me
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22755 Posts

Posted - 11/17/2012 :  04:50:09  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Go to the end of the page and find lot of other such methods
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

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.06 seconds. Powered By: Snitz Forums 2000