如何在PL / SQL中对多个字符使用replace命令

时间:2016-08-30 13:58:01

标签: oracle replace plsql

我正在编写一个程序,必须将一组特殊字符替换为应用程序系统接受的另一组特殊字符。如何更好地重写以下我在程序中使用的陈述?

select     replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace('%BICI* "(MOTO), |X PLAY? 4G: RED&WHITE& \/<DIRETTA>','(','-'),'%','perc'),'?','.'),'|','-'),':',';'),',','.'),'<','-'),'>','-'),'&','and'),'\','-'),'/','-'),'"','-'),')','-'),'*','-')
from dual;

我无法使用递归程序。 有什么建议吗?

谢谢! ILARIA

1 个答案:

答案 0 :(得分:0)

这是一个有趣的问题。这是一个基于Florin示例的工作示例:

with trans_tbl(id, symbol, txt) as (
  select 1, '(', '-'    from dual union
  select 2, '%', 'perc' from dual union
  select 3, '?', '.'    from dual union
  select 4, '|', '-'    from dual union
  select 5, ':', ';'    from dual union
  select 6, ',', '.'    from dual union
  select 7, '<', '-'    from dual union    
  select 8, '>', '-'    from dual union   
  select 9, '&', 'and'  from dual union   
  select 10, '\', '-'   from dual union    
  select 11, '/', '-'   from dual union
  select 12, '"', '-'   from dual union
  select 13, ')', '-'   from dual union
  select 14, '*', '-'   from dual 
),
data_tbl(str) as (
  select '%BICI* "(MOTO), |X PLAY? 4G: RED&WHITE& \/<DIRETTA>' from dual
),
working_tbl(str, id) as (
  SELECT str, 0 id 
  FROM data_tbl 
     UNION ALL
  SELECT replace(working_tbl.str,symbol,txt), trans_tbl.id
  FROM working_tbl 
  JOIN trans_tbl 
    ON working_tbl.id = trans_tbl.id - 1
)
--select str, id from working_tbl;
SELECT str 
from working_tbl 
where id = (select max(id) 
            from trans_tbl);

STR
----------------------------------------------------------
percBICI- --MOTO-. -X PLAY. 4G; REDandWHITEand ---DIRETTA-