2016-05-12 50 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