0

I have values in table like (a,b,c,d,e) and I want to remove any value as mentioned below.

suppose I want to remove b then comma(,) should move i.e (a,c,d,e). Please help.

0

3 Answers 3

2

Assuming that all your letters are separated by a single comma, you may use a combination of replace and trim functions.

--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    
Sign up to request clarification or add additional context in comments.

Comments

1

You can use either replace or regex_replace to achieve what you expect in plsql.

Select replace(MyTable.MyColumn, 'from value', 'to value') as NewValue from MyTable

1 Comment

How does this answer the question? The OP wants not just to replace a letter, but the associated commas too. I'm not sure why would someone upvote.
0

I borrowed code from Kaushik Nayak and added the regexp_replace mentioned by Khatibzadeh.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.