gpt4 book ai didi

sql - 在 Oracle Case When 中比较字符串

转载 作者:行者123 更新时间:2023-12-04 22:00:41 24 4
gpt4 key购买 nike

我有一个关于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com