简体   繁体   English

比较Oracle SQL中的两个空字符串

[英]Comparing two empty Strings in Oracle SQL

Hi today I have met with weird situation. 嗨,今天我遇到了奇怪的情况。 I had a where clause where was condition which returns String and I wanted to check if it's empty or not. 我有一个where子句,where条件是返回String的条件,我想检查它是否为空。 And when it returns empty string Oracle still treat it like a different Strings. 而且,当返回空字符串时,Oracle仍然将其视为不同的字符串。 So I went further and prepared simple queries: 因此,我走了一步,准备了简单的查询:

select 1 from dual where 1 = 1;
returns: 1

select 1 from dual where 'A' = 'A';
returns: 1

And now what I cannot understand: 现在我不明白的是:

select 1 from dual where '' = '';
No result.

Even if I check if they are different there is still no result. 即使我检查它们是否不同,仍然没有结果。

select 1 from dual where '' != '';
No result.

Can someone explain it for me ? 有人可以帮我解释一下吗?

Oracle treats empty strings as NULL. Oracle将空字符串视为NULL。 It's a gotcha. 这是一个陷阱。 Make a note of it and hope it never bites you in the butt in production. 记下它,并希望它永远不会在生产中对您造成伤害。

The reason is as @Captain Kenpachi explained. 原因是正如@Captain Kenpachi所解释的。 If want to compare two strings (or other types that are the same) and want to be tolerant of NULLs (or empty string in Oracle as it treats it as the same) then you need to involve an IS test. 如果要比较两个字符串(或其他相同的类型),并且要容忍NULL(或Oracle中的空字符串,因为它会将其视为相同),则需要进行IS测试。

You could try the common cheat of using a rogue value that will never be used but Murphy's Law dictates that one day someone will. 您可以尝试使用永远不会使用的流氓值来作弊,但墨菲定律规定某人会一天。 This technique also has the drawback that the rogue value should match the type of the thing you are comparing ie comparing strings you need a rogue string while comparing dates you need a rouge date. 该技术还具有一个缺点,即流氓值应与要比较的事物的类型匹配,即比较需要流氓字符串的字符串,而比较需要流氓日期的日期。 This also means you can't cut-and-paste it liberally without applying a little thought. 这也意味着您不能不加思索地随意剪切和粘贴它。 Example: WHERE NVL(col1,'MyRougeValue')=NVL(col2,'MyRougeValue') 示例: WHERE NVL(col1,'MyRougeValue')=NVL(col2,'MyRougeValue')

The standard version is to explicitly test for NULLs WHERE (col1=col2 OR (col1 IS NULL AND col2 IS NULL)) 标准版本将在其中显式测试NULL WHERE (col1=col2 OR (col1 IS NULL AND col2 IS NULL))

The opposite becomes WHERE NOT(col1=col2 OR (col1 IS NULL AND col2 IS NULL)) 相反的变为WHERE NOT(col1=col2 OR (col1 IS NULL AND col2 IS NULL))

I have seen the a long winded opposite version (as seen in Toad's data compare tool) WHERE (col1<>col2 OR (col1 IS NULL AND col2 IS NOT NULL) OR (col1 IS NOT NULL AND col2 IS NULL)) 我已经看到了一个相反的版本(在Toad的数据比较工具中可以看到) WHERE (col1<>col2 OR (col1 IS NULL AND col2 IS NOT NULL) OR (col1 IS NOT NULL AND col2 IS NULL))

Oracle does have a handy DECODE function that is basically is IF a IS b THEN c ELSE d so equality is WHERE DECODE(col1,col2,1,0)=1 and the opposite is WHERE DECODE(col1,col2,1,0)=0 . Oracle确实具有方便的DECODE函数,该函数基本上是在IF a IS b THEN c ELSE d因此相等性是WHERE DECODE(col1,col2,1,0)=1 ,而相反是WHERE DECODE(col1,col2,1,0)=0 You may find this a little slower than the explicit IS test. 您可能会发现这比显式IS测试要慢一些。 It is proprietary to Oracle but helps make up for the empty string problem. 它是Oracle专有的,但有助于弥补空字符串问题。

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

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