Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get current year only

Author  Topic 

jmersing
Yak Posting Veteran

57 Posts

Posted - 2011-09-27 : 09:05:45

I only want to return dates from the current year but I'm getting results from 2009 when I run this.

Starttime is a Datetime field, not null


and Datepart(yyyy, CONVERT(varchar,dbo.mytable.STARTTIME,110 )) >= Datepart(yyyy, GetDate())

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2011-09-27 : 09:12:55
quote:
Originally posted by jmersing


I only want to return dates from the current year but I'm getting results from 2009 when I run this.

Starttime is a Datetime field, not null


and Datepart(yyyy, CONVERT(varchar,dbo.mytable.STARTTIME,110 )) >= Datepart(yyyy, GetDate())






and Datepart(yyyy,STARTTIME) = Datepart(yyyy, GetDate())

------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page

jmersing
Yak Posting Veteran

57 Posts

Posted - 2011-09-27 : 09:15:17
That did it, Thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-09-27 : 10:26:40
it will be better if you do it as


and dbo.mytable.STARTTIME >= dateadd(year, datediff(year, 0, getdate()), 0)
and dbo.mytable.STARTTIME < dateadd(year, datediff(year, 0, getdate()) + 1, 0)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2011-09-27 : 11:22:23
quote:
Originally posted by khtan

it will be better if you do it as


and dbo.mytable.STARTTIME >= dateadd(year, datediff(year, 0, getdate()), 0)
and dbo.mytable.STARTTIME < dateadd(year, datediff(year, 0, getdate()) + 1, 0)



KH
[spoiler]Time is always against us[/spoiler]




Yes better you it like this.. in this way it will use index if created on datetime column.
Thanks for correct me.

------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page
   

- Advertisement -