Geeks With Blogs
Dennis Bottjer | MVP

Say you want to query a table returning all the records from a certain year.  An easy way to do this is to use SQL’s Year Function as follows:

 

Select * From <Table> Where Year(<DateTimeField>) = 2006

Posted on Saturday, December 10, 2005 1:12 AM SQL | Back to top


Comments on this post: SQL Query By Year

# re: SQL Query By Year
Requesting Gravatar...
if you have an index on that field, believe it or not it's slightly better to use something like:

select * from table where datetimefield between '1/1/2006 00:00:00' and '12/31/2006 11:59:59'

i believe it has to do with using a function in your where clause which is typically bad juju. in fact, the between will get converted to a >= and <= comparison by sql probably.
Left by royashbrook on Jan 19, 2007 6:11 AM

Your comment:
 (will show your gravatar)


Copyright © DennisBottjer | Powered by: GeeksWithBlogs.net