0

Oracle 11g:考虑以下两个 CASE 语句

搜索案例

with null_table as (select null as null_set from dual )
select 
     case 
     when (null_set is null) then 'NULL INDEED'
     else 'NOTNULL?'
     end as AM_I_NULL_OR_NOT   
from null_table

简单案例

with null_table as (select null as null_set from dual )
select 
     case null_set
     when  (null) then 'NULL INDEED'
     else 'NOTNULL?'
     end as AM_I_NULL_OR_NOT   
from null_table

Searched CASE 按预期评估null_set,但是 Simple CASE 似乎没有这样做。

问题:如何对null_set执行简单的 CASE 评估?

为什么 Simple CASE 会这样评估?

4

1 回答 1

3

在 Oracle 中,null 不能与使用=运算符进行比较。

例如:

select * from dual where null = null; -- No result
select * from dual where 1 = 1; -- gives a result

所以你可以做的是用NVL另一个值替换 null :

with null_table as (select null as null_set from dual )
select 
     case nvl(null_set,'X')
     when  'X' then 'NULL INDEED'
     else 'NOTNULL?'
     end as AM_I_NULL_OR_NOT   
from null_table

(确保您的字段永远不能等于示例中的值 X。)

于 2013-01-17T19:09:07.913 回答