Oracle PL/SQL 字符串比较问题
我有以下 Oracle PL/SQL 代码,从你们的角度来看可能很生疏:
DECLARE
str1 varchar2(4000);
str2 varchar2(4000);
BEGIN
str1:='';
str2:='sdd';
IF(str1<>str2) THEN
dbms_output.put_line('The two strings is not equal');
END IF;
END;
/
很明显,两个字符串 str1 和 str2 不相等,但为什么没有打印出“两个字符串不相等”? Oracle还有另一种比较两个字符串的常用方法吗?
I have the following Oracle PL/SQL codes that may be rusty from you guys perspective:
DECLARE
str1 varchar2(4000);
str2 varchar2(4000);
BEGIN
str1:='';
str2:='sdd';
IF(str1<>str2) THEN
dbms_output.put_line('The two strings is not equal');
END IF;
END;
/
This is very obvious that two strings str1 and str2 are not equal, but why 'The two strings are not equal' was not printed out? Do Oracle have another common method to compare two string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 Phil 所指出的,空字符串被视为 NULL,并且 NULL 不等于或不等于任何内容。如果您期望空字符串或 NULL,则需要使用
NVL()
处理它们:关于 null 比较:
根据 Oracle 12c 关于 NULL 的文档,使用 null 比较
IS NULL
或IS NOT NULL
的计算结果为TRUE
或FALSE
。然而,所有其他比较的结果都是UNKNOWN
,不是FALSE
。该文件进一步指出:Oracle 提供了一个参考表:
我还了解到,我们不应该编写 PL/SQL,假设空字符串将始终计算为 NULL:
As Phil noted, the empty string is treated as a NULL, and NULL is not equal or unequal to anything. If you expect empty strings or NULLs, you'll need to handle those with
NVL()
:Concerning null comparisons:
According to the Oracle 12c documentation on NULLS, null comparisons using
IS NULL
orIS NOT NULL
do evaluate toTRUE
orFALSE
. However, all other comparisons evaluate toUNKNOWN
, notFALSE
. The documentation further states:A reference table is provided by Oracle:
I also learned that we should not write PL/SQL assuming empty strings will always evaluate as NULL:
让我们通过在逻辑中添加其他分支来填补代码中的空白,然后看看会发生什么:
那么这两个字符串既不相同也不不相同?啊?
归根结底就是这样。 Oracle 将空字符串视为 NULL。如果我们尝试比较 NULL 和另一个字符串,结果既不是 TRUE 也不是 FALSE,而是 NULL。即使另一个字符串也是 NULL,情况仍然如此。
Let's fill in the gaps in your code, by adding the other branches in the logic, and see what happens:
So the two strings are neither the same nor are they not the same? Huh?
It comes down to this. Oracle treats an empty string as a NULL. If we attempt to compare a NULL and another string the outcome is not TRUE nor FALSE, it is NULL. This remains the case even if the other string is also a NULL.
我使用
=
而不是<>
来比较字符串。我发现在这种情况下=
似乎比<>
更合理。我已指定两个空(或 NULL)字符串相等。真正的实现返回 PL/SQL 布尔值,但这里我将其更改为 pls_integer(0 为 false,1 为 true)以便能够轻松演示该函数。I compare strings using
=
and not<>
. I've found out that in this context=
seems to work in more reasonable fashion than<>
. I have specified that two empty (or NULL) strings are equal. The real implementation returns PL/SQL boolean, but here I changed that to pls_integer (0 is false and 1 is true) to be able easily demonstrate the function.为了解决核心问题,“当其中一个为空时,我应该如何检测这两个变量不具有相同的值?”,我不喜欢
nvl(my_column, 'some value that永远、永远、永远不会出现在数据中,我可以绝对确定这一点')
,因为你不能总是保证某个值不会出现......尤其是数字。我使用了以下内容:
免责声明:我不是 Oracle 奇才,我自己想出了这个方案,并且在其他地方没有见过它,所以可能有一些微妙的原因说明它是一个坏主意。但它确实避免了 APC 提到的陷阱,即将 null 与其他内容进行比较既不会给出 TRUE 也不会给出 FALSE,而是给出 NULL。因为子句
(str1 is null)
将始终返回 TRUE 或 FALSE,而绝不会返回 null。(请注意,PL/SQL 执行短路评估,如此处所述。 )
To fix the core question, "how should I detect that these two variables don't have the same value when one of them is null?", I don't like the approach of
nvl(my_column, 'some value that will never, ever, ever appear in the data and I can be absolutely sure of that')
because you can't always guarantee that a value won't appear... especially with NUMBERs.I have used the following:
Disclaimer: I am not an Oracle wizard and I came up with this one myself and have not seen it elsewhere, so there may be some subtle reason why it's a bad idea. But it does avoid the trap mentioned by APC, that comparing a null to something else gives neither TRUE nor FALSE but NULL. Because the clauses
(str1 is null)
will always return TRUE or FALSE, never null.(Note that PL/SQL performs short-circuit evaluation, as noted here.)
我为此文本比较目的创建了一个存储函数:
在示例中:
I've created a stored function for this text comparison purpose:
In example:
只需更改线路即可
str1:='';
到
str1:='';
Only change the line
str1:='';
to
str1:=' ';
'' 将被视为 NULL,因此,两个字符串都需要检查为 NULL。
功能:
Sql语句:
The '' would be treated as NULL, so, both the strings need to be checked as NULL.
Function:
Sql Statement:
对于第一个问题:
可能该消息未打印出来,因为您关闭了输出。使用这些命令将其重新打开:
关于第二个问题:
我的 PLSQL 非常生锈,所以我无法给您完整的代码片段,但您需要循环 SQL 查询的结果集并 CONCAT 所有字符串一起。
To the first question:
Probably the message wasn't print out because you have the output turned off. Use these commands to turn it back on:
On the second question:
My PLSQL is quite rusty so I can't give you a full snippet, but you'll need to loop over the result set of the SQL query and CONCAT all the strings together.