Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a column [datatype:varchar(50)] in database (SQL Server 2008) having Values as shown below:

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

these are numbered bullets for my records I need to sort them as grouping all the records in sequence 1,1.1,1.1.1,2,3.1,4,10.1,11.1....

Please help me in this regard.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
333 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

Returns

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

Is that what you need?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...