Yohanne Posted October 15, 2016 Share Posted October 15, 2016 Hi coders, how to group by a month and year if i have a data date format like this. 10/23/2014 12/23/2015 11/23/2016 01/23/2016 01/23/2016 06/23/2014 06/23/2014 06/23/2014 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/302337-group-by-month-and-year-with-this-format-01292016/ Share on other sites More sharing options...
requinix Posted October 15, 2016 Share Posted October 15, 2016 Is this in a database? Quote Link to comment https://forums.phpfreaks.com/topic/302337-group-by-month-and-year-with-this-format-01292016/#findComment-1538301 Share on other sites More sharing options...
Yohanne Posted October 15, 2016 Author Share Posted October 15, 2016 Yes Quote Link to comment https://forums.phpfreaks.com/topic/302337-group-by-month-and-year-with-this-format-01292016/#findComment-1538304 Share on other sites More sharing options...
requinix Posted October 15, 2016 Share Posted October 15, 2016 Are you storing the dates as DATEs or VAR/CHARs? That's the type of the column. You need to be storing them as DATEs, and when you do you can use functions like MONTH, YEAR, and DATE_FORMAT. Quote Link to comment https://forums.phpfreaks.com/topic/302337-group-by-month-and-year-with-this-format-01292016/#findComment-1538305 Share on other sites More sharing options...
Yohanne Posted October 15, 2016 Author Share Posted October 15, 2016 i use VAR/CHARs and i think its too late to change it into DATE. its already have data more than 70k. Quote Link to comment https://forums.phpfreaks.com/topic/302337-group-by-month-and-year-with-this-format-01292016/#findComment-1538307 Share on other sites More sharing options...
Yohanne Posted October 15, 2016 Author Share Posted October 15, 2016 do you have any other solution on it? Quote Link to comment https://forums.phpfreaks.com/topic/302337-group-by-month-and-year-with-this-format-01292016/#findComment-1538308 Share on other sites More sharing options...
Barand Posted October 15, 2016 Share Posted October 15, 2016 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/302337-group-by-month-and-year-with-this-format-01292016/#findComment-1538309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.