How does SQL Server Wildcard Character Range, eg [A-D], work with Case-sensitive Collation?

后端 未结 4 1136
别跟我提以往
别跟我提以往 2020-12-11 03:57

Can anyone explain the rules for how a wildcard character range, eg [A-D], works with a case-sensitive collation?

I would have thought the following

         


        
4条回答
  •  抹茶落季
    2020-12-11 04:27

    You need a binary collation as indicated in Md. Elias Hossain's answer.

    The explanation is that ranges in the pattern syntax work off Collation sort order rules.

    From BOL

    In range searches, the characters included in the range may vary depending on the sorting rules of the collation.

    So

    ;WITH T(C) AS
    (
    SELECT 'A' UNION ALL
    SELECT 'B' UNION ALL
    SELECT 'C' UNION ALL
    SELECT 'D' UNION ALL
    select 'a' union all
    select 'b' union all
    select 'c' union all
    select 'd'
    )
    SELECT *
    FROM T
    ORDER BY C COLLATE Latin1_General_CS_AS
    

    Returns

    C
    ----
    a
    A
    b
    B
    c
    C
    d
    D
    

    So the range A-D excludes a but includes the other 3 lower case letters under a CS collation.

提交回复
热议问题