redconservatory
redconservatory

Reputation: 21934

mySQL select a column by MONTH

I have a column in my table that stores a date in the format:

2011-08-06 11:00:00

Is there a way to write an sql statement that selects row entries based on this column's MONTH?

Say for example, how would I select all entries in this table where COLUMN_NAME = "August"?

Upvotes: 0

Views: 1109

Answers (3)

Massenburger
Massenburger

Reputation: 51

You could say:

WHERE DATE_FORMAT(COLUMN_NAME, "%M") = "August"

Helpful page: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270767

To select based on the month's name, use:

WHERE DATE_FORMAT(COLUMN_NAME, '%M') = 'August'

To do it by month number, use

WHERE MONTH(COLUMN_NAME) = 8

Upvotes: 1

Jacob
Jacob

Reputation: 43299

You can use the MONTH() function.

SELECT * FROM table WHERE MONTH(COL_NAME) = 8

If you absolutely need it as a name, you can use MONTHNAME() the same way. But the returned name depends on the locale.

Upvotes: 4

Related Questions