{"id":821,"date":"2023-01-12T09:26:34","date_gmt":"2023-01-12T09:26:34","guid":{"rendered":"https:\/\/www.onfocus.co.uk\/blog\/?p=821"},"modified":"2023-01-12T09:28:33","modified_gmt":"2023-01-12T09:28:33","slug":"order-sql-results-by-the-month-part-of-the-date","status":"publish","type":"post","link":"https:\/\/www.onfocus.co.uk\/blog\/order-sql-results-by-the-month-part-of-the-date\/","title":{"rendered":"Order SQL results by the MONTH part of the DATE"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>The results as standard were coming out as follows:<\/p>\n\n\n\n<p><strong>2015<\/strong>-09-28<br><strong>2016<\/strong>-01-03<br><strong>2016<\/strong>-02-10<br><strong>2017<\/strong>-05-19<br><strong>2022<\/strong>-01-31<\/p>\n\n\n\n<p>This was by using the following Structured Query (SQL) code:<\/p>\n\n\n\n<p><code>$sql = \"SELECT * FROM entries <br>ORDER BY MY_Date\";<\/code><\/p>\n\n\n\n<p>Note that the year is ordered but for my purpose I require the SQL results to be ordered by the month as follows:<\/p>\n\n\n\n<p>2016-<strong>01<\/strong>-03<br>2022-<strong>01<\/strong>-31<br>2016-<strong>02<\/strong>-10<br>2017-<strong>05<\/strong>-19<br>2015-<strong>09<\/strong>-28<\/p>\n\n\n\n<p>To achieve this I have simply amended the code by adding the MONTH() function:<\/p>\n\n\n\n<p><code>$sql = \"SELECT * FROM entries <br>ORDER BY <strong>MONTH(<\/strong>MY_Date<strong>)<\/strong>\";<\/code><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#46;&#46;&#46;<\/p>\n","protected":false},"author":3,"featured_media":826,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[50],"class_list":["post-821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website-design","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=821"}],"version-history":[{"count":5,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/821\/revisions"}],"predecessor-version":[{"id":827,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/821\/revisions\/827"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}