• The following will handle more levels than you can shake a stick at with numbers as large as 99999999. You'll need the DelimitedSplit8K function for this and I've included a link to that article after the code below.

    --===== Build the test data

    DECLARE @Result TABLE (Serial nvarchar(10),Title varchar(20))

    INSERT INTO @Result

    SELECT '1.1','a' UNION ALL

    SELECT '1.2.1','b' UNION ALL

    SELECT '1.2.2','C' UNION ALL

    SELECT '1.2.3','G' UNION ALL

    SELECT '1.11','B' UNION ALL

    SELECT '2.3','B' UNION ALL

    SELECT '2.11','B' UNION ALL

    SELECT '2.2','C' UNION ALL

    SELECT '1.5','E' UNION ALL

    SELECT '1.3','E'

    ;

    --===== Solve the problem by creating a hierarchical sort path using a very high speed

    -- method for the right alignment of integer data (thanks, Dwaine)

    WITH

    cteBuildPath AS

    (

    SELECT r.Serial

    ,r.Title

    ,SortPath =

    (--==== Split the parts of each Serial and reassemble as a hierarchical path

    SELECT RIGHT(split.Item+100000000,8) --Converts each # to a zero filled right aligned number of 8 digits.

    FROM @Result r1

    CROSS APPLY dbo.DelimitedSplit8K(Serial,'.') split

    WHERE r1.Serial = r.Serial

    FOR XML PATH('')

    )

    FROM @Result r

    )

    SELECT Serial,Title

    FROM cteBuildPath

    ORDER BY SortPath

    ;

    The DelimitedSplit8K function may be found at the following URL...

    http://www.sqlservercentral.com/articles/Tally+Table/72993/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)