Compare String in Oracle Case When

橙三吉。 提交于 2019-12-08 01:17:59

问题


i have an issue with oracle case when.

SELECT CASE WHEN '7C54D3E133830A78E040A8C010014B7D' != ''
            THEN '7C54D3E133830A78E040A8C010014B7D'
            WHEN 'e84a4433966c4b8996ce34905acff63d' != ''
            THEN 'e84a4433966c4b8996ce34905acff63d'
            WHEN '7faa9126b1c6412fa58375ab2b2be1db' != ''
            THEN '7faa9126b1c6412fa58375ab2b2be1db'
            ELSE NULL
 END
 FROM DUAL

this query always returns null, though it's obvious the result should be the first case. Am i missing something about string comparison in oracle?


回答1:


You are checking strings againts an empty string, thus having issues; in Oracle you'd better check if your string is not 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 

About the way Oracle treats empty string and null, here you find something more

An example:

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

gives:

'' = ''           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

In a word, the only check you can rely on, when talking about NULL, is: IS [NOT] NULL




回答2:


Well, the reason of such a behaviour is that Oracle doesn't have empty string, but null; that's why

   select case when 'abc' != ''  
      ....

is actually

   select case when 'abc' != null

and since anything != null is null (true, false, null boolean logic) all the when don't return true and else is executed. The right syntax is

   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


来源:https://stackoverflow.com/questions/41758158/compare-string-in-oracle-case-when

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!