Reputation: 7673
I have a table that holds items with a created_at
DATETIME
type column. I would like to return all items that were made in 2013.
I think I could do:
SELECT * FROM `items`
WHERE `created_at` > '2012' AND `created_at` < '2014'
But I would ideally like to be able to do something like:
SELECT * FROM `items`
WHERE `created_at` = '2013'
I think this fails because it looks for items with a DATETIME
of 2013-01-01 00:00:00. Is there a way to do it by converting the MySQL created_at
to a year when comparing?
Upvotes: 1
Views: 3151
Reputation: 257
use DATE_FORMAT mysql function.
SELECT * FROM `items`
WHERE DATE_FORMAT(created_at, ,'%Y') = '2013'
Upvotes: 1
Reputation: 46910
SELECT * FROM items WHERE YEAR(created_at) = 2013
Reference: MySQL
Upvotes: 3
Reputation: 57670
Use year
function for created_at
column
WHERE YEAR(`created_at`) = '2013'
Upvotes: 5