简体   繁体   English

SQL 类似和通配符与 Oracle SQL 开发人员

[英]SQL Like and Wildcard with Oracle SQL Developer

I am using SQL Developer over a backend Oracle DB.我在后端 Oracle DB 上使用 SQL Developer。 I have a record where the buyer name is Pete Hansen.我有记录,买家的名字是皮特汉森。

Why I try为什么我尝试

select * from data1 where buyer = 'Pete Hansen';

I get the result, no problem.我得到结果,没问题。 However, when I use the following, I do not get any results:但是,当我使用以下内容时,我没有得到任何结果:

select * from data1 where buyer  like 'Pete Hansen';

Also, when I try the following, I do not get any results另外,当我尝试以下操作时,我没有得到任何结果

select * from data1 where buyer like 'Pete Hans_n';

but the following works well:但以下效果很好:

select * from data1 where buyer like 'Pete Hans_n%';

Could you please help me understand?你能帮我理解吗? Thanks in advance.提前致谢。

I suspect it may have to do with trailing white spaces, which like operator is not forgiving about but = is.我怀疑它可能与尾随空格有关, like运算符不会原谅,但=是。 To test that, try要测试它,请尝试

select * from data1 where trim(buyer) like 'Pete Hansen';

Your buyer column seems to be defined as char ;您的buyer栏似乎被定义为char you can see the issue reproduced in this db<>fiddle , but not when the column is varchar2 .您可以看到此 db<>fiddle 中重现的问题,但当varchar2时不会。

The documentation for character comparison explains the difference between blank-padded or nonpadded comparison semantics. 字符比较的文档解释了空白填充或非填充比较语义之间的区别。 When you compare them with = , because both the column and the string literal are `char, blank-padded semantics are used:当您将它们与=进行比较时,因为列和字符串文字都是 `char,所以使用空白填充语义:

With blank-padded semantics, if the two values have different lengths, then Oracle first adds blanks to the end of the shorter one so their lengths are equal.使用空白填充语义,如果两个值的长度不同,则 Oracle 首先将空白添加到较短的末尾,以便它们的长度相等。 ... If two values have no differing characters, then they are considered equal. ...如果两个值没有不同的字符,那么它们被认为是相等的。 This rule means that two values are equal if they differ only in the number of trailing blanks.此规则意味着如果两个值仅在尾随空格的数量上有所不同,则它们是相等的。 Oracle uses blank-padded comparison semantics only when both values in the comparison are either expressions of data type CHAR, NCHAR, text literals, or values returned by the USER function. Oracle 仅当比较中的两个值都是数据类型 CHAR、NCHAR、文本文字的表达式或 USER function 返回的值时,才使用空白填充的比较语义。

When the column is `varchar2 then nonpadded semantics are used:当列是 `varchar2 时,使用非填充语义:

With nonpadded semantics, ... If two values of equal length have no differing characters, then the values are considered equal.使用非填充语义,... 如果两个长度相等的值没有不同的字符,则认为这些值相等。 Oracle uses nonpadded comparison semantics whenever one or both values in the comparison have the data type VARCHAR2 or NVARCHAR2.只要比较中的一个或两个值具有数据类型 VARCHAR2 或 NVARCHAR2,Oracle 就会使用非填充比较语义。

LIKE works differently. LIKE的工作方式不同。 Only your final pattern with % matches, because that is allowing for the trailing spaces in the char value, while the other two patterns do not.只有您的最终模式与%匹配,因为这允许char值中的尾随空格,而其他两种模式则不允许。 With the varchar2 version there aren't any trailing spaces to the other two patterns also match.对于varchar2版本,其他两种模式也没有任何尾随空格。

It's unusual to need or want to user char columns;需要或想要使用char列是不寻常的; varchar2 is more usual. varchar2更常见。 Tom Kyte opined on this many years ago.多年前,汤姆·凯特(Tom Kyte)对此提出了看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM