Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select * from column where year = 2010

Tags:

sql

select * from mytable where year(Columnx) = 2010

Regarding index usage (answering Simon's comment):

if you have an index on Columnx, SQLServer WON'T use it if you use the function "year" (or any other function).

There are two possible solutions for it, one is doing the search by interval like Columnx>='01012010' and Columnx<='31122010' and another one is to create a calculated column with the year(Columnx) expression, index it, and then do the filter on this new column


If i understand that you want all rows in the year 2010, then:

select * 
  from mytable 
 where Columnx >= '2010-01-01 00:00:00' 
       and Columnx < '2011-01-01 00:00:00'

T-SQL and others;

select * from t where year(Columnx) = 2010

its just simple

  select * from myTable where year(columnX) = 2010