如何在一个中合并两个解码语句。

时间:2014-08-04 10:02:54

标签: sql oracle plsql

如果以下条件匹配

,我选择让我们说两个表中的user_name
user_name=DECODE (krt.user_name,' ',' ', RTRIM (krt.user_name))
user_name=DECODE(length(krt.user_name),10,krt.user_name,16,substr(krt.user_name,6,10))

如何合并这两个陈述。?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

使用CASE声明:

with krt as (
  select ' ' user_name from dual union all
  select '1234567890' from dual union all
  select '1234567890123456' from dual
)
select case
         when krt.user_name = ' ' then krt.user_name
         when length( RTRIM (krt.user_name)) = 10 then  RTRIM (krt.user_name)
         when length( RTRIM (krt.user_name)) = 16 then substr( RTRIM (krt.user_name),6,10)
       end
  from krt