记录一次oracle 19c null比较引发的问题

-- A='', B='', A和B会转为null,比较结果xxx:ccc, A既不等于B,也不不等于B, 处于不可比较的一种状态
with t1 as (select '' a, '' b from dual )
select t1.*, case when t1.a=t1.b then 'aaa' when t1.a!=t1.b then 'bbb' else 'ccc' end xxx from t1;

-- A='', B='1', A会转为null, 比较结果xxx:ccc, 虽然B有值,但仍然处于不可比较的一种状态
with t1 as (select '' a, '1' b from dual )
select t1.*, case when t1.a=t1.b then 'aaa' when t1.a!=t1.b then 'bbb' else 'ccc' end xxx from t1;

-- A='', B=null, A会转为null, 比较结果xxx:ccc, 仍然处于不可比较的一种状态
with t1 as (select '' a, null b from dual )
select t1.*, case when t1.a=t1.b then 'aaa' when t1.a!=t1.b then 'bbb' else 'ccc' end xxx from t1;

-- A=null, B=null, 比较结果xxx:ccc, 仍然处于不可比较的一种状态
with t1 as (select null a, null b from dual )
select t1.*, case when t1.a=t1.b then 'aaa' when t1.a!=t1.b then 'bbb' else 'ccc' end xxx from t1;

-- 结论: oracle 的null 不可比较, 或许可以转换一下使用

with t1 as (select '' a, '' b from dual )
select t1.*, case when nvl(t1.a, '9999')=nvl(t1.b, '9999') then 'aaa'
                  when nvl(t1.a, '9999')!=nvl(t1.b, '9999') then 'bbb'
                  else 'ccc'
    end xxx from t1;

with t1 as (select '' a, '' b from dual )
select t1.*, case when decode(t1.a, '', '2')=decode(t1.b, '', '2') then 'aaa'
                  when decode(t1.a, '', '2')!=decode(t1.b, '', '2') then 'bbb' else 'ccc' end xxx from t1;

参考:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements005.htm

描述如下:

If you use any other condition with nulls and the result depends on the value of the null, then the result is UNKNOWN. Because null represents a lack of data, a null cannot be equal or unequal to any value or to another null. However, Oracle considers two nulls to be equal when evaluating a DECODE function. Please refer to DECODE for syntax and additional information.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值