2013-06-25 32 views
0

我有以下代碼:甲骨文案例當一個像

case when (a.je_source='Revaluation') then 'No_Location' 
    when d.user_name like ('SCHE%') then 'No_Location' 
    when d.user_name like ('C-FA%') then 'No_Location' 
    when d.user_name like ('C-AGO%') then 'No_Location' 
    when d.user_name like ('C-VD%') then 'No_Location' 
    when d.user_name like ('C-JL%') then 'No_Location' 
    else d.user_name 
end as JE_User 

有沒有一種方法,使之清潔?我嘗試了以下,並收到一個缺少右括號的錯誤。

case when (a.je_source='Revaluation') then 'No_Location' 
    when d.user_name like ('SCHE%', 'C-FA%') then 'No_Location' 
    else d.user_name 
end as JE_User 
+0

沒有,沒有......你可以把一個或替代的所有條件,但就是這樣。 – Ben

+0

謝謝Ben!如果有更好的方法,我想確保我使用它。 – Fenzl

+0

不是我所知道的。 – xQbert

回答

3

作爲一個選項,你可以使用(Oracle 10g及以上)regexp_like條件:

 -- sample of data 
SQL> with t1(je_source, user_name) as(
    2 select 'Revaluation1', 'SCHE123' from dual union all 
    3 select 'Revaluation2', 'C-FABCD' from dual union all 
    4 select 'Revaluation3', 'C-AGOABC' from dual union all 
    5 select 'Revaluation4', 'C-VD'  from dual union all 
    6 select 'Revaluation5', 'C-JLABC' from dual union all 
    7 select 'Revaluation', 'ABCDE' from dual union all 
    8 select 'Revaluation6', 'FGHIJ' from dual 
    9 ) 
10 select je_source 
11  , user_name 
12  , case 
13   when je_source = 'Revaluation' 
14   then 'No_Location' 
15   when regexp_like(user_name, '^SCHE\w*|^C-FA\w*|^C-AGO\w*|^C-VD\w*|^C-JL\w*', 'i') 
16   then 'No_Location' 
17   else user_name 
18   end case 
19 from t1 
20/

JE_SOURCE USER_NAME CASE 
------------ --------- ----------- 
Revaluation1 SCHE123 No_Location 
Revaluation2 C-FABCD No_Location 
Revaluation3 C-AGOABC No_Location 
Revaluation4 C-VD  No_Location 
Revaluation5 C-JLABC No_Location 
Revaluation ABCDE  No_Location 
Revaluation6 FGHIJ  FGHIJ 

7 rows selected