Oracle 中不区分大小写的搜索
LIKE
和其他比较运算符 =
等的默认行为区分大小写。
是否可以使它们不区分大小写?
The default behaviour of LIKE
and the other comparison operators, =
etc is case-sensitive.
Is it possible make them case-insensitive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
也许你可以尝试使用
maybe you can try using
从 Oracle 12c R2 开始,您可以使用
整理运算符
:演示:
db<>fiddle 演示
From Oracle 12c R2 you could use
COLLATE operator
:Demo:
db<>fiddle demo
如果将 COLLATE 运算符放在表达式的末尾,它也可以工作,这对我来说似乎更干净。
所以你可以使用这个:
而不是这个:
无论如何,我喜欢 COLLATE 运算符解决方案,原因如下:
The COLLATE operator also works if you put it at the end of the expression, and that seems cleaner to me.
So you can use this:
instead of this:
Anyhow, I like the COLLATE operator solution for the following reasons:
你可以这样做:
you can do something like that:
在 Oracle 中,有 3 种主要方法可以在不使用全文索引的情况下执行不区分大小写的搜索。
最终选择哪种方法取决于您的个人情况;要记住的主要事情是,为了提高性能,您必须正确索引以进行不区分大小写的搜索。
1. 将列和字符串的大小写保持一致。
您可以使用 强制所有数据大小写相同
UPPER()
或LOWER()
:或者
如果
column_1
未在upper(column_1)
或lower(column_1) 上建立索引
,根据情况,这可能会强制进行全表扫描。为了避免这种情况,您可以创建一个 基于函数的索引。如果您使用 LIKE,则必须在要搜索的字符串周围连接一个
%
。这个 SQL Fiddle 演示了所有这些查询中发生的情况。请注意解释计划,它指示何时使用索引以及何时不使用索引。
2.使用正则表达式。
从 Oracle 10g 开始
REGEXP_LIKE()
可用。您可以指定 _match_parameter_'i'
,以执行不区分大小写的搜索。为了将其用作相等运算符,您必须指定字符串的开头和结尾,用克拉和美元符号表示。
为了执行 LIKE 的等效功能,可以删除这些。
请小心这一点,因为您的字符串可能包含正则表达式引擎会以不同方式解释的字符。
此 SQL Fiddle 显示相同的示例输出,但使用 REGEXP_LIKE() 除外。
3. 在会话级别更改它。
NLS_SORT 参数控制排序的排序顺序以及各种比较运算符,包括
=
和 LIKE。您可以通过更改会话来指定二进制、不区分大小写的排序。这意味着在该会话中执行的每个查询都将执行不区分大小写的参数。关于语言排序和字符串搜索有大量其他信息如果您想指定不同的语言,或者使用 BINARY_AI 进行不区分重音的搜索。
您还需要更改 NLS_COMP 参数;引用:
NLS_COMP的默认值为BINARY;但是,LINGUISTIC指定Oracle应该注意NLS_SORT的值:
因此,您需要再次更改会话
如文档中所述,您可能想要创建一个 语言索引以提高性能
There are 3 main ways to perform a case-insensitive search in Oracle without using full-text indexes.
Ultimately what method you choose is dependent on your individual circumstances; the main thing to remember is that to improve performance you must index correctly for case-insensitive searching.
1. Case your column and your string identically.
You can force all your data to be the same case by using
UPPER()
orLOWER()
:or
If
column_1
is not indexed onupper(column_1)
orlower(column_1)
, as appropriate, this may force a full table scan. In order to avoid this you can create a function-based index.If you're using LIKE then you have to concatenate a
%
around the string you're searching for.This SQL Fiddle demonstrates what happens in all these queries. Note the Explain Plans, which indicate when an index is being used and when it isn't.
2. Use regular expressions.
From Oracle 10g onwards
REGEXP_LIKE()
is available. You can specify the _match_parameter_'i'
, in order to perform case-insensitive searching.In order to use this as an equality operator you must specify the start and end of the string, which is denoted by the carat and the dollar sign.
In order to perform the equivalent of LIKE, these can be removed.
Be careful with this as your string may contain characters that will be interpreted differently by the regular expression engine.
This SQL Fiddle shows you the same example output except using REGEXP_LIKE().
3. Change it at the session level.
The NLS_SORT parameter governs the collation sequence for ordering and the various comparison operators, including
=
and LIKE. You can specify a binary, case-insensitive, sort by altering the session. This will mean that every query performed in that session will perform case-insensitive parameters.There's plenty of additional information around linguistic sorting and string searching if you want to specify a different language, or do an accent-insensitive search using BINARY_AI.
You will also need to change the NLS_COMP parameter; to quote:
The default value of NLS_COMP is BINARY; but, LINGUISTIC specifies that Oracle should pay attention to the value of NLS_SORT:
So, once again, you need to alter the session
As noted in the documentation you may want to create a linguistic index to improve performance
从 10gR2 开始,Oracle 允许通过设置 <代码>NLS_COMP和
NLS_SORT
会话参数:
您还可以创建不区分大小写的索引:
此信息取自 Oracle 不区分大小写的搜索。文章提到了
REGEXP_LIKE
但它似乎也适用于旧的=
。在早于 10gR2 的版本中,这实际上是不可能完成的,如果您不需要不区分重音的搜索,通常的方法就是只
UPPER()
列和搜索表达式。Since 10gR2, Oracle allows to fine-tune the behaviour of string comparisons by setting the
NLS_COMP
andNLS_SORT
session parameters:You can also create case insensitive indexes:
This information was taken from Oracle case insensitive searches. The article mentions
REGEXP_LIKE
but it seems to work with good old=
as well.In versions older than 10gR2 it can't really be done and the usual approach, if you don't need accent-insensitive search, is to just
UPPER()
both the column and the search expression.