Friday, December 09, 2005 8:12 PM dbottjer

SQL Query By Year

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

Filed under:

Comments

# re: SQL Query By Year

Friday, January 19, 2007 12:11 AM by royashbrook

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.