Accent and case insensitive collation in Oracle with LIKE

后端 未结 1 1457
情深已故
情深已故 2020-12-17 02:21

I have found this answer useful: Accent and case insensitive COLLATE equivalent in Oracle, but my question is regarding LIKE searching with a version 9 Oracle db.

I

相关标签:
1条回答
  • 2020-12-17 02:30

    one method would be to modify your session parameters NLS_SORT and NLS_COMP:

    SQL> SELECT Name FROM CollationTestTable WHERE NAME LIKE '%pe%';
    
    NAME
    --------------------------------------------------------------------------------
    pepe
    
    SQL> alter session set nls_sort=Latin_AI;
    
    Session altered
    
    SQL> alter session set nls_comp=linguistic;
    
    Session altered
    
    SQL> SELECT Name FROM CollationTestTable WHERE NAME LIKE '%pe%';
    
    NAME
    --------------------------------------------------------------------------------
    pepe
    pépé
    PEPE
    

    As shown in another SO, you cannot use the LIKE operator with NLSSORT (this is because, NLSSORT returns a string of bytes that will be used for sorting, and LIKE only works with charater strings)

    Update: While setting the NLS parameters would be my first choice, you could also use built-in functions to achieve the same result. A couple of examples:

    SQL> SELECT Name
      2    FROM CollationTestTable
      3   WHERE upper(convert(NAME, 'US7ASCII'))
      4         LIKE upper(convert('%pe%', 'US7ASCII'));
    
    NAME
    --------------------------------------------------------------------------------
    pepe
    pépé
    PEPE
    
    SQL> SELECT Name
      2    FROM CollationTestTable
      3   WHERE upper(translate(NAME, 'àâéèêìîòôùûÿ', 'aaeeeiioouuy'))
      4         LIKE upper(translate('%pe%', 'àâéèêìîòôùûÿ', 'aaeeeiioouuy'));
    
    NAME
    -----------------------------------
    pepe
    pépé
    PEPE
    
    0 讨论(0)
提交回复
热议问题