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

后端 未结 4 1125
别跟我提以往
别跟我提以往 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:21

    It can be done in either way:

    a. Use COLLATE while create the table as:

    CREATE TABLE #TEST_LIKE_Patterns
    ( 
        ID INT IDENTITY(1,1),
        CharColumn VARCHAR(100) COLLATE Latin1_General_BIN
    );
    

    b. Use COLLATE while selecting data as

    SELECT *
    FROM #TEST_LIKE_Patterns
    WHERE CharColumn LIKE '%[A-D]%' COLLATE Latin1_General_BIN;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-11 04:27

    Using a case-sensitive collation works for search strings that are not in a range e.g. this would work:

    SELECT *
      FROM #TEST_LIKE_Patterns
     WHERE (
            CharColumn LIKE 'A%' COLLATE Latin1_General_CS_AS
            OR CharColumn LIKE 'B%' COLLATE Latin1_General_CS_AS
            OR CharColumn LIKE 'C%' COLLATE Latin1_General_CS_AS
            OR CharColumn LIKE 'D%' COLLATE Latin1_General_CS_AS
           );
    

    ...but clearly that's not an acceptable approach!

    As others have suggested, use Latin1_General_BIN for ranges.

    0 讨论(0)
  • 2020-12-11 04:35

    try

    SELECT *
    FROM #TEST_LIKE_Patterns
    WHERE CharColumn  LIKE '[A-D]%' COLLATE Latin1_General_BIN;
    
    0 讨论(0)
提交回复
热议问题