SQL Sorting and hyphens

I learned something new, just like you as well

I believe the difference is between a “String Sort” vs a “Word Sort” (ignores hyphen)

Sample difference between WORD sort and STRING sort
http://andrusdevelopment.blogspot.com/2007/10/string-sort-vs-word-sort-in-net.html

From Microsoft
http://support.microsoft.com/kb/322112

For example, if you are using the SQL
collation
“SQL_Latin1_General_CP1_CI_AS”, the
non-Unicode string ‘a-c’ is less than
the string ‘ab’ because the hyphen
(“-“) is sorted as a separate
character that comes before “b”.
However, if you convert these strings
to Unicode and you perform the same
comparison, the Unicode string N’a-c’
is considered to be greater than N’ab’
because the Unicode sorting rules use
a “word sort” that ignores the hyphen.

I did some sample code
you can also play with the COLLATE to find the one to work with your sorting

DECLARE @test TABLE
(string VARCHAR(50))

INSERT INTO @test SELECT 'co-op'
INSERT INTO @test SELECT 'co op'
INSERT INTO @test SELECT 'co_op'

SELECT * FROM @test ORDER BY string --COLLATE SQL_Latin1_General_Cp1_CI_AS
--co op
--co-op
--co_op

SELECT * FROM @test ORDER BY CAST(string AS NVARCHAR(50)) --COLLATE SQL_Latin1_General_Cp1_CI_AS
--co op
--co_op
--co-op

Leave a Comment