Order SQL results by the MONTH part of the DATE
I needed to display a set of retrieved database results ordered by the MONTH part of the DATE so that the list would be in month order and not by the year.
The results as standard were coming out as follows:
2015-09-28
2016-01-03
2016-02-10
2017-05-19
2022-01-31
This was by using the following Structured Query (SQL) code:
$sql = "SELECT * FROM entries
ORDER BY MY_Date";
Note that the year is ordered but for my purpose I require the SQL results to be ordered by the month as follows:
2016-01-03
2022-01-31
2016-02-10
2017-05-19
2015-09-28
To achieve this I have simply amended the code by adding the MONTH() function:
$sql = "SELECT * FROM entries
ORDER BY MONTH(MY_Date)";
Recent Comments