November 25, 2025 at 10:16 am
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.
November 25, 2025 at 11:01 am
Interestingly, using only VARCHAR produces consistent results. Not sure why Unicode characters are working differently.
November 25, 2025 at 12:15 pm
I get the same (wrong) results, VARCHAR or NVARCHAR makes no difference.
November 25, 2025 at 12:45 pm
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
November 25, 2025 at 12:57 pm

I got exactly the same result as with using NVARCHAR.
November 25, 2025 at 1:42 pm
As I am running exactly the same version of SQL Server as you, I can only conclude that your query settings must somehow be affecting the result. Not sure I can help any further.
November 25, 2025 at 1:59 pm
Yes, you pointed me to the right direction. It is a collation thing.
SELECT Id
FROM @s
ORDER BY Id COLLATE Latin1_General_CI_AI
SELECT Id
FROM @s
ORDER BY Id COLLATE SQL_Latin1_General_CP1_CI_AI
My database has collation Latin1_General_CI_AI, which gives the wrong results when using VARCHAR. For NVARCHAR all results are the same and wrong.
November 25, 2025 at 1:59 pm
Yes, you pointed me to the right direction. It is a collation thing.
SELECT Id
FROM @s
ORDER BY Id COLLATE Latin1_General_CI_AI
SELECT Id
FROM @s
ORDER BY Id COLLATE SQL_Latin1_General_CP1_CI_AI
My database has collation Latin1_General_CI_AI, which gives the wrong results when using VARCHAR. For NVARCHAR all results are the same and wrong.
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply