作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于 oracle 案例的问题。
SELECT CASE WHEN '7C54D3E133830A78E040A8C010014B7D' != ''
THEN '7C54D3E133830A78E040A8C010014B7D'
WHEN 'e84a4433966c4b8996ce34905acff63d' != ''
THEN 'e84a4433966c4b8996ce34905acff63d'
WHEN '7faa9126b1c6412fa58375ab2b2be1db' != ''
THEN '7faa9126b1c6412fa58375ab2b2be1db'
ELSE NULL
END
FROM DUAL
此查询始终返回 null,但很明显结果应该是第一种情况。我是否缺少有关 oracle 中字符串比较的内容?
最佳答案
您正在检查字符串是否为空字符串,因此有问题;在 Oracle 中,你最好检查你的字符串 是否不为 null
:
SELECT CASE WHEN '7C54D3E133830A78E040A8C010014B7D' is not null
THEN '7C54D3E133830A78E040A8C010014B7D'
WHEN 'e84a4433966c4b8996ce34905acff63d' is not null
THEN 'e84a4433966c4b8996ce34905acff63d'
WHEN '7faa9126b1c6412fa58375ab2b2be1db' is not null
THEN '7faa9126b1c6412fa58375ab2b2be1db'
ELSE NULL
END
FROM DUAL
关于 Oracle 处理空字符串和 null 的方式,here你会发现更多东西
一个例子:
select q'['' = '']' , case when '' = '' then 'YES' else 'NO' end from dual union all
select q'['' is null]' , case when '' is null then 'YES' else 'NO' end from dual union all
select q'['' = null ]' , case when '' = null then 'YES' else 'NO' end from dual union all
select q'[null = null]' , case when null = null then 'YES' else 'NO' end from dual union all
select q'[null is null]' , case when null is null then 'YES' else 'NO' end from dual union all
select q'['' != '']' , case when '' != '' then 'YES' else 'NO' end from dual union all
select q'['' is not null]' , case when '' is not null then 'YES' else 'NO' end from dual union all
select q'['' != null ]' , case when '' != null then 'YES' else 'NO' end from dual union all
select q'[null != null]' , case when null != null then 'YES' else 'NO' end from dual union all
select q'[null is not null]', case when null is not null then 'YES' else 'NO' end from dual
给出:
'' = '' NO
'' is null YES
'' = null NO
null = null NO
null is null YES
'' != '' NO
'' is not null NO
'' != null NO
null != null NO
null is not null NO
简而言之,在谈论 NULL
时,您唯一可以依赖的检查是: 是 [NOT] NULL
关于sql - 在 Oracle Case When 中比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758158/
我是一名优秀的程序员,十分优秀!