我在数据库(SQL Server 2008)中有一个列[datatype:varchar(50)]
,其值如下所示:
1
2
1.1.11
4.1
5
2.1
1.1
4
1.2.1
4.2.2
4.3
4.2
4.3.1
4.2.1
11.2
1.2.4
4.4
这些是我记录的编号子弹
我需要将它们排序为按顺序1,1.1,1.1.1,2,3.1,4,10.1,11.1....
请帮助我。
答案 0 :(得分:18)
WITH T(YourColumn) AS
(
SELECT '1' UNION ALL
SELECT '2' UNION ALL
SELECT '1.1.11' UNION ALL
SELECT '4.1' UNION ALL
SELECT '5' UNION ALL
SELECT '2.1' UNION ALL
SELECT '1.1' UNION ALL
SELECT '4' UNION ALL
SELECT '1.2.1' UNION ALL
SELECT '4.2.2' UNION ALL
SELECT '4.3' UNION ALL
SELECT '4.2' UNION ALL
SELECT '4.3.1' UNION ALL
SELECT '4.2.1' UNION ALL
SELECT '11.2' UNION ALL
SELECT '1.2.4' UNION ALL
SELECT '4.4'
)
SELECT *
FROM T
ORDER BY CAST('/' + YourColumn + '/' AS HIERARCHYID)
返回
YourColumn
----------
1
1.1
1.1.11
1.2.1
1.2.4
2
2.1
4
4.1
4.2
4.2.1
4.2.2
4.3
4.3.1
4.4
5
11.2
这就是你需要的吗?