Sort Bullets in Database

前端 未结 1 1204
闹比i
闹比i 2020-11-28 14:30

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.         


        
相关标签:
1条回答
  • 2020-11-28 15:20
    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?

    0 讨论(0)
提交回复
热议问题