Case insensitive searching in Oracle

前端 未结 6 560
予麋鹿
予麋鹿 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:56

    Since 10gR2, Oracle allows to fine-tune the behaviour of string comparisons by setting the NLS_COMP and NLS_SORT session parameters:

    SQL> SET HEADING OFF
    SQL> SELECT *
      2  FROM NLS_SESSION_PARAMETERS
      3  WHERE PARAMETER IN ('NLS_COMP', 'NLS_SORT');
    
    NLS_SORT
    BINARY
    
    NLS_COMP
    BINARY
    
    
    SQL>
    SQL> SELECT CASE WHEN 'abc'='ABC' THEN 1 ELSE 0 END AS GOT_MATCH
      2  FROM DUAL;
    
             0
    
    SQL>
    SQL> ALTER SESSION SET NLS_COMP=LINGUISTIC;
    
    Session altered.
    
    SQL> ALTER SESSION SET NLS_SORT=BINARY_CI;
    
    Session altered.
    
    SQL>
    SQL> SELECT *
      2  FROM NLS_SESSION_PARAMETERS
      3  WHERE PARAMETER IN ('NLS_COMP', 'NLS_SORT');
    
    NLS_SORT
    BINARY_CI
    
    NLS_COMP
    LINGUISTIC
    
    
    SQL>
    SQL> SELECT CASE WHEN 'abc'='ABC' THEN 1 ELSE 0 END AS GOT_MATCH
      2  FROM DUAL;
    
             1
    

    You can also create case insensitive indexes:

    create index
       nlsci1_gen_person
    on
       MY_PERSON
       (NLSSORT
          (PERSON_LAST_NAME, 'NLS_SORT=BINARY_CI')
       )
    ;
    

    This information was taken from Oracle case insensitive searches. The article mentions REGEXP_LIKE but it seems to work with good old = as well.


    In versions older than 10gR2 it can't really be done and the usual approach, if you don't need accent-insensitive search, is to just UPPER() both the column and the search expression.

提交回复
热议问题