Case insensitive searching in Oracle

前端 未结 6 557
予麋鹿
予麋鹿 2020-11-22 16:52

The default behaviour of LIKE and the other comparison operators, = etc is case-sensitive.

Is it possible make them case-insensitive?

6条回答
  •  孤独总比滥情好
    2020-11-22 17:45

    From Oracle 12c R2 you could use COLLATE operator:

    The COLLATE operator determines the collation for an expression. This operator enables you to override the collation that the database would have derived for the expression using standard collation derivation rules.

    The COLLATE operator takes one argument, collation_name, for which you can specify a named collation or pseudo-collation. If the collation name contains a space, then you must enclose the name in double quotation marks.

    Demo:

    CREATE TABLE tab1(i INT PRIMARY KEY, name VARCHAR2(100));
    
    INSERT INTO tab1(i, name) VALUES (1, 'John');
    INSERT INTO tab1(i, name) VALUES (2, 'Joe');
    INSERT INTO tab1(i, name) VALUES (3, 'Billy'); 
    --========================================================================--
    SELECT /*csv*/ *
    FROM tab1
    WHERE name = 'jOHN' ;
    -- no rows selected
    
    SELECT /*csv*/ *
    FROM tab1
    WHERE name COLLATE BINARY_CI = 'jOHN' ;
    /*
    "I","NAME"
    1,"John"
    */
    
    SELECT /*csv*/ *
    FROM tab1 
    WHERE name LIKE 'j%';
    -- no rows selected
    
    SELECT /*csv*/ *
    FROM tab1 
    WHERE name COLLATE BINARY_CI LIKE 'j%';
    /*
    "I","NAME"
    1,"John"
    2,"Joe"
    */
    

    db<>fiddle demo

提交回复
热议问题