按值组排序

时间:2017-01-23 21:39:19

标签: sql-server

我在表格中有一个位置列表。第一个位置以'2'开头并以'8'结束,第二个位置以字母'A'开头并以'P'结束。最后,每个位置都有七个元素,如下例所示:

SELECT Location FROM WAREHOUSE_LOCATIONS 

Location |
---------|
2A-1     |
2A-2     | 
2A-3     |
2A-4     |
2A-5     |
2A-6     |
2A-7     |
2B-1     |
2B-2     |
2B-3     |
2B-4     |
2B-5     |
2B-6     |
2B-7     |
2C-1     |
...
3A-1     |
...
4A-1     |
...
etc...

我想订购这样的地点:

Location |
---------|
2A-1     |
2B-1     | 
2C-1     |
2D-1     |
....
2P-1     |
2A-2     |
2B-2     | 
2C-2     |
...
2P-2     |
2A-3     |
2B-3     |
2C-3     |

我该怎么做?

1 个答案:

答案 0 :(得分:3)

你可以这样做:

ORDER BY CAST(RIGHT(Location, LEN(Location) - CHARINDEX('-', Location)) AS INT),
LEFT(Location, CHARINDEX('-', Location) - 1)