use*_*951 5 sql t-sql database sql-server sql-server-2005
我在SQL中有以下排序问题.
SELECT time, orderValue
FROM orders
ORDER BY time
Run Code Online (Sandbox Code Playgroud)
问题是时间用以下格式的字符串表示:
May 2012
June 2012
...
June 2013
Run Code Online (Sandbox Code Playgroud)
然而,ORDER BY子句按字母顺序对问题进行排序(这并不奇怪,因为它被定义为字符串).如何根据年份和月份以正确的顺序对此进行排序?
尝试:
SELECT time, orderValue
FROM orders
ORDER BY CONVERT (DATETIME, '01 ' + time, 104)
Run Code Online (Sandbox Code Playgroud)