简体   繁体   中英

Compare String in Oracle Case When

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM