在 oracle SQL 查询中使用字符串包含函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20346551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use string contains function in oracle SQL query
提问by ibaneight
I'm using an Oracle database and I want to know how can I find rows in a varchar type column where the values of that column has a string which contains some character.
我正在使用 Oracle 数据库,我想知道如何在 varchar 类型列中查找行,其中该列的值具有包含某些字符的字符串。
I'm trying something like this (that's a simple example of what I want), but it doesn't work:
我正在尝试这样的事情(这是我想要的一个简单示例),但它不起作用:
select p.name
from person p
where p.name contains the character 'A';
I also want to know if I can use a function like chr(1234)
where 1234 is an ASCII code instead of the 'A'
character in my example query, because in my case I want to search in my database values where the name of a person contains the character with 8211 as ASCII code.
我还想知道我是否可以使用一个函数chr(1234)
,其中 1234 是一个 ASCII 代码而不是'A'
我的示例查询中的字符,因为在我的情况下,我想在我的数据库值中搜索一个人的名字包含 8211 的字符作为 ASCII 码。
With the query select CHR(8211) from dual;
I get the special character that I want.
通过查询,select CHR(8211) from dual;
我得到了我想要的特殊字符。
Example:
例子:
select p.name
from person p
where p.name contains the character chr(8211);
回答by ADTC
By linesI assume you mean rows in the table person
. What you're looking for is:
通过行我假设你在表中平均行person
。你要找的是:
select p.name
from person p
where p.name LIKE '%A%'; --contains the character 'A'
The above is case sensitive. For a case insensitive search, you can do:
以上是区分大小写的。对于不区分大小写的搜索,您可以执行以下操作:
select p.name
from person p
where UPPER(p.name) LIKE '%A%'; --contains the character 'A' or 'a'
For the special character, you can do:
对于特殊字符,您可以执行以下操作:
select p.name
from person p
where p.name LIKE '%'||chr(8211)||'%'; --contains the character chr(8211)
The LIKE
operator matches a pattern. The syntax of this command is described in detail in the Oracle documentation. You will mostly use the %
sign as it means match zero or more characters.
该LIKE
运营商的模式相匹配。该命令的语法在Oracle 文档中有详细描述。您将主要使用该%
符号,因为它意味着匹配零个或多个字符。
回答by ibaneight
The answer of ADTC works fine, but I've find another solution, so I post it here if someone wants something different.
ADTC 的答案工作正常,但我找到了另一个解决方案,所以如果有人想要不同的东西,我会在这里发布。
I think ADTC's solution is better, but mine's also works.
我认为 ADTC 的解决方案更好,但我的也有效。
Here is the other solution I found
这是我找到的另一个解决方案
select p.name
from person p
where instr(p.name,chr(8211)) > 0; --contains the character chr(8211)
--at least 1 time
Thank you.
谢谢你。