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
 select rows with datetime field by year

Author  Topic 

kieran5405
Yak Posting Veteran

96 Posts

Posted - 2008-01-21 : 11:16:05
Hi,

I have rows with a datetime column which spans many years. I am wondering is there a more simple way to select those rows where the datetime column is in a particular year. For example...
where datetime_column.year = '2007'

I know i can use the 'between' sql etc...but am wondering if theres something more straightforward im not aware of.

Thanks..

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-21 : 11:25:22
where datetime_column >= '20070101'
and datetime_column < '20080101'



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

kieran5405
Yak Posting Veteran

96 Posts

Posted - 2008-01-21 : 11:41:28
cheers for feedback peso...i came across something that works also i.e. where year(datetime_Column) = 2007
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-01-21 : 11:52:26
quote:
Originally posted by kieran5405

cheers for feedback peso...i came across something that works also i.e. where year(datetime_Column) = 2007



That is not a good practice, because it will prevent the use of any index on the datetime_column.

The method that Peso posted is a much better way to query for a datetime range.


CODO ERGO SUM
Go to Top of Page

subrata4allfriends
Starting Member

24 Posts

Posted - 2008-01-21 : 13:21:53
quote:
Originally posted by kieran5405

cheers for feedback peso...i came across something that works also i.e. where year(datetime_Column) = 2007



Yes, its the easier process....but whatever Michael said is the fact....I also agree with him.

Thanks & Regards,
4allfriends.

"Life is not a bed of roses."
Go to Top of Page
   

- Advertisement -