Oracle 中使用 LIKE 进行重音和大小写不敏感排序规则

发布于 2024-08-08 02:02:34 字数 2316 浏览 4 评论 0原文

我发现这个答案很有用: Oracle 中的重音和不区分大小写的 COLLATE 等效项,但我的问题关于使用版本 9 Oracle 数据库进行 LIKE 搜索。

我尝试过这样的查询:

SELECT column_name
FROM table_name
WHERE NLSSORT(column_name, 'NLS_SORT = Latin_AI')
LIKE NLSSORT('%somethingInDB%', 'NLS_SORT = Latin_AI')

但没有返回任何结果。

我创建了一个小Java文件来测试:

import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DbCollationTest
{
 public static void main(String[] args) throws SQLException
 {
  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
  dataSource.setUrl("url");
  dataSource.setUsername("usr");
  dataSource.setPassword("pass");

  Connection conn = null;
  PreparedStatement createStatement = null;
  PreparedStatement populateStatement = null;
  PreparedStatement queryStatement = null;
  PreparedStatement deleteStatement = null;
  ResultSet rs = null;

  try
  {
   conn = dataSource.getConnection();

   createStatement = conn.prepareStatement("CREATE TABLE CollationTestTable ( Name varchar(255) )");
   createStatement.execute();

   String[] names = { "pepe", "pépé", "PEPE", "MEME", "mémé", "meme" };
   int i = 1;

   for (String name : names)
   {
    populateStatement = conn.prepareStatement("INSERT INTO CollationTestTable VALUES (?)");
    populateStatement.setString(1, name);
    populateStatement.execute();
   }

   queryStatement = conn.prepareStatement("SELECT Name FROM CollationTestTable WHERE NLSSORT(NAME, 'NLS_SORT = Latin_AI') LIKE NLSSORT('%pe%', 'NLS_SORT = Latin_AI')");
   rs = queryStatement.executeQuery();

   while (rs.next())
   {
    System.out.println(rs.getString(1));
   }

   deleteStatement = conn.prepareStatement("DROP TABLE CollationTestTable");
   deleteStatement.execute();
  }
  finally
  {
   //DBTools.tidyUp(conn, null, rs);
   //DBTools.tidyUp(createStatement);
   //DBTools.tidyUp(populateStatement);
   //DBTools.tidyUp(queryStatement);
   //DBTools.tidyUp(deleteStatement);
  }
 }
}

我没有成功谷歌搜索,有人有任何解决方案吗?

我想对部分名称执行搜索,并返回不区分大小写和重音的匹配结果。

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 have tried a query like this:

SELECT column_name
FROM table_name
WHERE NLSSORT(column_name, 'NLS_SORT = Latin_AI')
LIKE NLSSORT('%somethingInDB%', 'NLS_SORT = Latin_AI')

but no results are ever returned.

I created a little Java file to test:

import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DbCollationTest
{
 public static void main(String[] args) throws SQLException
 {
  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
  dataSource.setUrl("url");
  dataSource.setUsername("usr");
  dataSource.setPassword("pass");

  Connection conn = null;
  PreparedStatement createStatement = null;
  PreparedStatement populateStatement = null;
  PreparedStatement queryStatement = null;
  PreparedStatement deleteStatement = null;
  ResultSet rs = null;

  try
  {
   conn = dataSource.getConnection();

   createStatement = conn.prepareStatement("CREATE TABLE CollationTestTable ( Name varchar(255) )");
   createStatement.execute();

   String[] names = { "pepe", "pépé", "PEPE", "MEME", "mémé", "meme" };
   int i = 1;

   for (String name : names)
   {
    populateStatement = conn.prepareStatement("INSERT INTO CollationTestTable VALUES (?)");
    populateStatement.setString(1, name);
    populateStatement.execute();
   }

   queryStatement = conn.prepareStatement("SELECT Name FROM CollationTestTable WHERE NLSSORT(NAME, 'NLS_SORT = Latin_AI') LIKE NLSSORT('%pe%', 'NLS_SORT = Latin_AI')");
   rs = queryStatement.executeQuery();

   while (rs.next())
   {
    System.out.println(rs.getString(1));
   }

   deleteStatement = conn.prepareStatement("DROP TABLE CollationTestTable");
   deleteStatement.execute();
  }
  finally
  {
   //DBTools.tidyUp(conn, null, rs);
   //DBTools.tidyUp(createStatement);
   //DBTools.tidyUp(populateStatement);
   //DBTools.tidyUp(queryStatement);
   //DBTools.tidyUp(deleteStatement);
  }
 }
}

I have not had any success googling, anyone have any solutions?

I want to perform a search on part of a name and return results that are matched using case and accent insensitivity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-08-15 02:02:34

一种方法是修改会话参数 NLS_SORTNLS_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

另一个时将使用的索引SO,您不能将 LIKE 运算符与 NLSSORT 一起使用(这是因为,NLSSORT 返回将用于排序的字节字符串,LIKE 仅适用于字符字符串)

更新:虽然设置 NLS 参数是我的第一选择,但您也可以使用内置函数来实现相同的结果。举几个例子:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文