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

标签 sql oracle case

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

相关文章:

sql - ORA-01756 : quoted string not properly terminated when I try to execute my code

c# - Oracle 和 SQL Server 之间的多线程数据传输 - 网络性能

if-statement - 二郎: nested cases

mysql - 这是 MySQL JOIN 吗?

SQL查询按字母顺序排序

mysql - 是否可以将一列引用为多个外键?

c# - Sql参数和c#

java - ORA-00905: 缺少关键字 Hibernate 查询 Oracle

MySQL SELECT CASE WHEN 获取速度

mysql - 如何根据下表选择和分组mysql数据