How to do a like case-insensitive and accent insensitive in Oracle 10gR2 and JPA?

前端 未结 3 1193
名媛妹妹
名媛妹妹 2021-01-11 17:52

In a J2EE project, using JPA, how can I force a like query to be case insensitive and accent insensitive?

I know about changing session variables NLS_COMP and NLS_SO

相关标签:
3条回答
  • 2021-01-11 18:13

    Crudely, you can do something like

    select  upper(convert('This is a têst','US7ASCII')),
            upper(convert('THIS is A test','US7ASCII'))
    from dual;
    
    select  1 from dual 
    where upper(convert('This is a têst','US7ASCII')) =
                 upper(convert('THIS is A test','US7ASCII'))
    

    The CONVERT reduces the accented characters to the mapped ASCII equivalent, and the UPPER forces lower case to uppercase. The resultant strings should be matchable.

    0 讨论(0)
  • 2021-01-11 18:28

    (...) using JPA, how can I force a like query to be case insensitive and accent insensitive?

    My answer will be JPQL oriented. For the former part, you could do:

    where lower(name) like 'johny%';
    

    For the later part, I'm not aware of a standard JPQL way to do it.

    At the end, altering the session variables NLS_COMP and NLS_SORT is IMO the best option.

    0 讨论(0)
  • 2021-01-11 18:36

    You could use NLS_UPPER for that without altering the session:

    select 1
    from dual
    where nls_upper('große', 'NLS_SORT = XGerman') like '%OSSE%';
    

    NLS_UPPER documentation

    0 讨论(0)
提交回复
热议问题