chris
chris

Reputation: 36937

MySQL select specific year or year month rows that have yyyy-mm-dd as the date

Ok, Im sure this is simple and its just evading me at the moment. But Im searching and I can't find it either, maybe I don't know what Im searching for term wise. Anyway.

I have a table, with rows of posts, and within those rows is a column thats in YYYY-MM-DD format, might be YYYY-MM-DD HH:MM:SS either way. I'm Trying to build a query that will let me select either just the entries in a specific year, or specific month of a given year. But since the formats are what they are, Im confusing myself, I know theres a way to format the dates in the query so they will be easy to compare against. But I cant think of it.

My thought process is seriously flawed currently. at the moment on how to handle that.

Note I am using Codeigniter and its ORM as well

Upvotes: 2

Views: 9745

Answers (1)

iLaYa  ツ
iLaYa ツ

Reputation: 4017

You could try this code

FROM_UNIXTIME(datetime_column, '%Y') AS year

or to fetch year specific record

 YEAR(datetime_column)='2012'

to fetch month specific record

 MONTH(datetime_column)='11'

Upvotes: 6

Related Questions