all groups > sql server programming > july 2006 >
You're in the

sql server programming

group:

Dictionary Sorting Weirdness


Dictionary Sorting Weirdness Dan
7/19/2006 7:34:01 PM
sql server programming:
I encountered an interesting anomaly in a sort operation with SQL Server. It
appears that the dictionary sort order will place a lower case b ahead of a
upper case B. I ran the following script and got the strange results:

CREATE TABLE MySortTable
(DictionarySort nvarchar(10) COLLATE Latin1_General_CI_AS NULL)
GO
INSERT MySortTable
VALUES ('Alpha')
INSERT MySortTable
VALUES ('Bravo')
INSERT MySortTable
VALUES ('Charlie')
INSERT MySortTable
VALUES ('alpha')
INSERT MySortTable
VALUES ('bravo')
INSERT MySortTable
VALUES ('charlie')
GO
SELECT DictionarySort FROM MySortTable
ORDER BY DictionarySort

The results show the following:
Alpha
alpha
bravo
Bravo
Charlie
charlie

Re: Dictionary Sorting Weirdness Dan
7/19/2006 9:19:01 PM
Duh!, Thanks Tom. It makes perfect sense. I guess the part that threw me was
that all the other values came back as I expected them, upper case then lower
case. It was just the "b" that kept jumping ahead in line. I understand the
case insensitivity, I just got tunnel vision because of the consistent order
of results except the B. I ran it on several different servers and they all
returned the same order as I showed in my post. Thanks again.

[quoted text, click to view]
Re: Dictionary Sorting Weirdness Omnibuzz
7/19/2006 10:04:01 PM
Just an additional point.. You will get a better ordering (as u had expected)
if u try this :)

SELECT DictionarySort FROM MySortTable
ORDER BY DictionarySort collate Latin1_General_CS_AS

(This is a case-sensitive collation)

--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/



[quoted text, click to view]
Re: Dictionary Sorting Weirdness Tom Cooper
7/19/2006 11:20:22 PM
Latin1_General_CI_AS is case insensitive. That is, 'Bravo' is equal to
'bravo' for sorting and comparision purposes. So one might come first, or
the other might come first. SQL Server is allowed to choose whichever one
it wants to, and a different set of data, or a different service pack or
many other things might have the result in a different order. This is
equivalent to having two rows with an integer column, both of the rows
having a 2 in that column, selecting the data ordered by that column and
asking "Why did SQL Server put this row with a 2 in front of that row with a
2?"

Tom

[quoted text, click to view]

AddThis Social Bookmark Button