enable-case-insensitive-string-comparison-in-oracle-12c

 

By default, Oracle checks content as well as casing, while comparing the two strings in "where" or "like" or "case" statements.

For example, the statement given below will return 0.

  1. SELECT CASE WHEN 'abc'='ABC' THEN 1 ELSE 0 END AS MATCHED FROM DUAL;  

Since Oracle compares the content and casing as well, it will return 0. We can ignore case and compare only content by altering user session by using NLS_COMP and NLS_SORT parameters.

To set these parameters, we can run the statements, given below, in SQL*Plus or SQL Developer:

  1. ALTER SESSION SET NLS_COMP=LINGUISTIC;  
  2. ALTER SESSION SET NLS_SORT=BINARY_CI;  

Setting NLS_COMP to LINGGUISTIC means the comparisons for all SQL operations in the "where" clause and in PL/SQL blocks should use the linguistic sort specified in the NLS_SORT parameter and ignore the character case in the string comparison.

Setting NLS_SORT to BINARY_CI means SQL operations will use binary value for sorting and ignore the character case.



After setting the parameters, given above, the string comparison will ignore the character case. Since we can’t set these parameters at the database level, we can create the database trigger, given below, to set these parameters on a user login:

  1. CREATE OR REPLACE TRIGGER SYSTEM. CHANGE_NLS_COMPARE_STRING  
  2. AFTER LOGON ON DATABASE  
  3. WHEN (  
  4. USER='SYSTEM'  
  5. )  
  6. begin  
  7. execute immediate ' ALTER SESSION SET NLS_COMP=LINGUISTIC';  
  8. execute immediate ' ALTER SESSION SET NLS_SORT=BINARY_CI';  
  9. end ;  

Before using this trigger, please evaluate for any performance issues in the string sort, comparison, and user login. For more details, please refer here.

I am ending the things here. I hope this blog will be helpful for all.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章