日期:2014-05-17  浏览次数:21236 次

case when 为空时如何处理
SELECT CASE
  WHEN '' IS NULL THEN 222
  ELSE 0
  END
  FROM DUAL;

这条语句可以正常执行 ,但是下面这条语句就无法正常执行了,请大虾指点

(case receive_detail.USER_DEFINED_03 --列名
  when 'NORMAL' then 'Good'
  when 'BAD' then 'Damaged'
  when '' is null then '/'
  else receive_detail.USER_DEFINED_03
  end) as status,

------解决方案--------------------
(case receive_detail.USER_DEFINED_03 --列名
when 'NORMAL' then 'Good'
when 'BAD' then 'Damaged'
when null then '/'
else receive_detail.USER_DEFINED_03
end) as status
------解决方案--------------------
SQL code
(case when receive_detail.USER_DEFINED_03 ='NORMAL' then 'Good'
when receive_detail.USER_DEFINED_03 ='BAD' then 'Damaged'
when receive_detail.USER_DEFINED_03 is null then '/'
else receive_detail.USER_DEFINED_03
end) as status

------解决方案--------------------
你是case...when...和case when...没搞清楚
看下书吧,两个用法不一样的
case...when...里的when后面只能接值,不能接表达式!
case when...的when后面只能接表达式不能接值!
------解决方案--------------------
SQL code

--为何不用decode函数呢?
decode(receive_detail.USER_DEFINED_03,'NORMAL','Good',
       'BAD','Damaged',
       '','/',
       receive_detail.USER_DEFINED_03) as status

------解决方案--------------------
不会吧,我这个试过了,行的啊
SQL code
with a as(select '1' d from dual union all
select '2'  from dual union all
select '5'  from dual union all
select ''  from dual 
)
select case when d='1' then '11' 
when d='2' then '22' 
when d is null then '/' 
else d end from a