我有一个(varchar)类型的列,其中包含连字符。连字符后的数字代表一年。我需要先按年份排序。
11-18
10-18
11-16
12-17
10-19
应该这样排序:
11-16
12-17
10-18
11-18
10-19
答案 0 :(得分:1)
也许是这样的
示例
Select *
From YourTable
Order By right(SomeCol,2),SomeCol
返回
SomeCol
11-16
12-17
10-18
11-18
10-19
答案 1 :(得分:1)
尝试这个
DECLARE @t AS TABLE(CODE VARCHAR(10));
INSERT INTO @T
SELECT '11-18'
UNION ALL
SELECT '10-18'
UNION ALL
SELECT '11-16'
UNION ALL
SELECT '12-17'
UNION ALL
SELECT '10-19';
SELECT *
FROM @t
ORDER BY SUBSTRING(CODE, 4, 2),
SUBSTRING(CODE, 1, 2);