SQL Server 2022 order by bug?

  • Hi,

    I was using order by on a column with characters and saw something I couldn't explain. Seems a bug to me, or am I missing something.

    SELECT @@VERSION

    SELECT N'1.' Id
    UNION ALL
    SELECT N'1-' Id
    ORDER BY Id

    SELECT N'1.w' Id
    UNION ALL
    SELECT N'1-w' Id
    ORDER BY Id

    DECLARE @s TABLE (Id NVARCHAR(3))
    INSERT @s (Id)
    SELECT N'1.' Id
    UNION ALL
    SELECT N'1-' Id
    UNION ALL
    SELECT N'1.w' Id
    UNION ALL
    SELECT N'1-w' Id

    SELECT Id
    FROM @s
    ORDER BY Id

    Results:

    Microsoft SQL Server 2022 (RTM-CU22) (KB5068450) - 16.0.4225.2 (X64)

    Oct 22 2025 23:44:36

    Copyright (C) 2022 Microsoft Corporation

    Web Edition (64-bit) on Windows Server 2016 Standard 10.0 <X64> (Build 14393: ) (Hypervisor)

     

    Id

    ----

    1-

    1.

     

    Id

    ----

    1.w

    1-w

     

    Id

    ----

    1-

    1.

    1.w

    1-w

    I would expect that the ids with '-' always be listed before the ids with '.'.

    Any ideas on this?

    Best regards, Hugo.

  • Interestingly, using only VARCHAR produces consistent results. Not sure why Unicode characters are working differently.


  • I get the same (wrong) results, VARCHAR or NVARCHAR makes no difference.

  • This is the code I ran. Can you confirm that it produces inconsistent results on your system?

    SELECT @@VERSION

    SELECT '1.' Id
    UNION ALL
    SELECT '1-' Id
    ORDER BY Id

    SELECT '1.w' Id
    UNION ALL
    SELECT '1-w' Id
    ORDER BY Id

    DECLARE @s TABLE (Id VARCHAR(3))
    INSERT @s (Id)
    SELECT '1.' Id
    UNION ALL
    SELECT '1-' Id
    UNION ALL
    SELECT '1.w' Id
    UNION ALL
    SELECT '1-w' Id

    SELECT Id
    FROM @s
    ORDER BY Id

  • Query Results

    I got exactly the same result as with using NVARCHAR.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply