2016-05-12 73 views
0

我有下面的WHERE子句和我看到哪里哪里的End_dt2是空白查询没有返回数据结果甲骨文 - 比较两列<>

我怎样才能返回,其中第一列不以相等空白?

下面的值不返回。
表:

id End_dt End_dt2 
1  1/1/15 

Where End_dt <> End_dt2 

回答

1

空比较总是给出问题。您应该添加NVL处理是:

WHERE End_dt <> NVL(End_dt2, date '9999-12-31') 

这当然,考虑到当End_dt2为空,则记录应出现在您的查询。

+0

你可以尝试WHERE END_DT <> NVL(End_dt2,END_DT - 1)的情况下,有人开始使用END_DT与您的选择相符的神奇价值......也许使用COALESCE(),而不是NVL() –

5

如果end_dt2为null,则任何比较都是未定义的,但不匹配。 Null behaviour is explained in the documentation。你可以把空值作为一个神奇的日期,但你也可以检查空明确:

where end_dt2 is null or end_dt != end_dt2 

如果任一列可以为空,你要正确对待一个(而不是两个)是空的“不同”,那么你可以这样做:

where (end_dt is not null and end_dt2 is null) 
or (end_dt is null and and_dtl2 is not null) 
or end_dt <> end_dt2