How do I extract Month and Year in a MySQL date and compare them?
How do I extract the month and date from a mySQL date and compare it to another date?
I found this MONTH() but it only gets the month. I looking for month and year.
in Mysql Doku: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_extract
SELECT EXTRACT( YEAR_MONTH FROM `date` )
FROM `Table` WHERE Condition = 'Condition';
While it was discussed in the comments, there isn't an answer containing it yet, so it can be easy to miss. DATE_FORMAT works really well and is flexible to handle many different patterns.
DATE_FORMAT(date,'%Y%m')
To put it in a query:
SELECT DATE_FORMAT(test_date,'%Y%m') AS date FROM test_table;
If you are comparing between dates, extract the full date for comparison. If you are comparing the years and months only, use
SELECT YEAR(date) AS 'year', MONTH(date) AS 'month'
FROM Table Where Condition = 'Condition';