Author |
Topic |
qutesanju
Posting Yak Master
193 Posts |
Posted - 2009-02-05 : 08:55:44
|
how to find year end and year start from date entered? |
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-02-05 : 09:05:08
|
select dateadd(d,-datepart(dy,getdate())+1,getdate()) as yearstart, dateadd(d,-datepart(d,getdate()),dateadd(m,13-datepart(m,getdate()),getdate())) as yearendJai Krishna |
|
|
Rajesh Jonnalagadda
Starting Member
45 Posts |
Posted - 2009-02-05 : 09:37:32
|
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 YearEndRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url] |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-02-05 : 10:04:21
|
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 YearEndRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url]
I think change is not that necessary as my query is producing correct resultsJai Krishna |
|
|
jek2201
Starting Member
1 Post |
Posted - 2009-05-26 : 04:40:44
|
A simple one if you want also to have hours, minuttes and the seconds right as well:--Year Startselect 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'))) |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-05-26 : 04:55:15
|
hai rajesh, both queries will give u same outputselect dateadd(d,-datepart(dy,getdate())+1,getdate()) as yearstart,dateadd(d,-datepart(d,getdate()),dateadd(m,13-datepart(m,getdate()),getdate())) as yearendSELECT DateAdd(d, -DatePart(dy,getdate()-1),getdate()) as YearStart, DateAdd(d, -DatePart(d,getdate()), Dateadd(m,13-datepart(m,getdate()),getdate())) as YearEnd |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-26 : 05:11:50
|
Keep it simpleSELECT 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" |
|
|
HenryJin
Starting Member
1 Post |
Posted - 2012-11-07 : 20:59:49
|
SwePeso's code is working perfect for me |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|