如何解决带有LIKE的Oracle中的重音和不区分大小写的排序规则
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
解决方法
我发现此答案很有用:
Oracle中口音和不区分大小写的COLLATE等效,但是我的问题是关于使用版本9 Oracle
db进行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);
}
}
}
我没有在谷歌搜索上取得任何成功,没有任何解决方案?
我想对名称的一部分执行搜索,并返回使用大小写和重音不敏感匹配的结果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。