我在表中有(a,b,c,d,e)
之类的值,并且我要删除任何如下所述的值。
假设我要删除b
,然后逗号(,
)应该移动,即(a,c,d,e)
。请帮忙。
答案 0 :(得分:2)
假设所有字母都用一个逗号分隔,则可以结合使用replace
和trim
函数。
--Test data
with t(s,r) AS
(
select 'a,b,c,d,e', 'b' from dual union all
select 'a,b,c,d,e', 'e' from dual union all
select 'a,b,c,d,e', 'a' from dual union all
select 'a,b,c,d,e', 'c' from dual union all
select 'a,b,c,d,e', 'd' from dual union all
select 'a,bc,c,d,e', 'bc' from dual union all
select 'ad,bc,c,d,ef','ef' from dual union all
select 'ad,bc,c,d,ef','ad' from dual
)
select s, r as to_remove,
trim ( both ',' from replace(','||s||',', ','||r||',' ,',') ) removed
from t;
S TO_REMOVE REMOVED
a,b,c,d,e b a,c,d,e
a,b,c,d,e e a,b,c,d
a,b,c,d,e a b,c,d,e
a,b,c,d,e c a,b,d,e
a,b,c,d,e d a,b,c,e
a,bc,c,d,e bc a,c,d,e
ad,bc,c,d,ef ef ad,bc,c,d
ad,bc,c,d,ef ad bc,c,d,ef
答案 1 :(得分:1)
您可以使用replace或regex_replace来实现您在plsql中所期望的功能。
Select replace(MyTable.MyColumn, 'from value', 'to value') as NewValue from MyTable
答案 2 :(得分:0)
我从Kaushik Nayak借来了代码,并添加了Khatibzadeh提到的regexp_replace。
with t(s,r) AS
(
select 'a,b,c,d,e', 'b' from dual union all
select 'a,b,c,d,e', 'e' from dual union all
select 'a,b,c,d,e', 'a' from dual union all
select 'a,b,c,d,e', 'c' from dual union all
select 'a,b,c,d,e', 'd' from dual
)
select
trim ( both ',' from replace(','||s||',', ','||r||',' ,',') ) trim_replace,
regexp_replace(s, '^' || r || ',|,' || r || '$|,' || r, '') regexp_replace
from t