SQL Sorting and hyphens

前端 未结 1 1856
余生分开走
余生分开走 2020-12-01 19:36

Is there a way to easily sort in SQL Server 2005 while ignoring hyphens in a string field? Currently I have to do a REPLACE(fieldname,\'-\',\'\') or a function to remove th

相关标签:
1条回答
  • 2020-12-01 20:10

    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
    
    0 讨论(0)
提交回复
热议问题