gpt4 book ai didi

java - Oracle 中带有 LIKE 的重音和不区分大小写的排序规则

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:36 24 4
gpt4 key购买 nike

我发现这个答案很有用: Accent and case insensitive COLLATE equivalent in Oracle ,但我的问题是关于使用第 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);
}
}
}

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

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

最佳答案

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

如图another SO ,您不能将 LIKE 运算符与 NLSSORT 一起使用(这是因为 NLSSORT returns a string of bytes 将用于排序,而 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

关于java - Oracle 中带有 LIKE 的重音和不区分大小写的排序规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1572577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com