Sort by month expressed as a string in SQL

I have the following sorting problem in SQL.

SELECT time, orderValue FROM orders ORDER BY time 

The problem is that time is expressed as a string in the following format:

 May 2012 June 2012 ... June 2013 

The ORDER BY clause, however, sorts the problem in alphabetical order (which is not strange since it is defined as a string). How to sort this in the correct order by year and month?

+4
source share
1 answer

Try:

 SELECT time, orderValue FROM orders ORDER BY CONVERT (DATETIME, '01 ' + time, 104) 
+8
source

Source: https://habr.com/ru/post/1488446/


All Articles