作爲替代,您可以使用NVL
功能和指定的文字將被退回,如果值爲null:
-- both are not nulls
SQL> with t1(col1, col2) as(
2 select 123, 123 from dual
3 )
4 select 1 res
5 from t1
6 where nvl(col1, -1) = nvl(col2, -1)
7 ;
RES
----------
1
-- one of the values is null
SQL> with t1(col1, col2) as(
2 select null, 123 from dual
3 )
4 select 1 res
5 from t1
6 where nvl(col1, -1) = nvl(col2, -1)
7 ;
no rows selected
-- both values are nulls
SQL> with t1(col1, col2) as(
2 select null, null from dual
3 )
4 select 1 res
5 from t1
6 where nvl(col1, -1) = nvl(col2, -1)
7 ;
RES
----------
1
由於@Codo的評論所指出的,當然,上述方法需要選擇一個文字比較列永遠不會有。如果比較列是數字數據類型(例如)並且能夠接受任何值,那麼選擇-1當然不會成爲選項。爲了消除對於這個限制,我們可以使用decode
功能(數字或字符數據類型):
with t1(col1, col2) as(
2 select null, null from dual
3 )
4 select 1 res
5 from t1
6 where decode(col1, col2, 'same', 'different') = 'same'
7 ;
RES
----------
1
另外相關http://stackoverflow.com/questions/636886/sql-not-equals-null –
太糟糕了,Oracle不支持標準'IS NOT DISTINCT FROM'運算符。 –