Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Trier les puces dans la base de données

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)

Retours

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

C'est ce dont vous avez besoin ?