Jump to content

group by month and year with this format "01/29/2016"


Yohanne

Recommended Posts

Use str_to_date() to convert your format to to DATE type. Then you can use the other datetime functions on it.

EG

SELECT
    YEAR(STR_TO_DATE(datestr, '%m/%d/%Y')) as yr
,   MONTH(STR_TO_DATE(datestr, '%m/%d/%Y')) as mth
,   COUNT(*) as total
FROM mytable
GROUP BY yr, mth

You can use the same function to update your database table with a single query.

  • Backup the table, just in case
  • UPDATE mytable SET datestr = STR_TO_DATE(datestr, '%m/%d/%Y');
  • Alter varchar to DATE
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.