简体   繁体   中英

Remove special characters from a string in oracle sql

The field contains special characters like pipe,returns ,* and ( example:

Table Name: Employee
Field names: id, name
ID   Name
01   Radhika N (MBA)*MCA*
02   Venu (BBA)
03   (MBA)Ananth
04   Visal **MCA**

Now i want a select statement that removes all special characters and my result should be

ID   Name
01  RADHIKA N
02  VENU
03  ANANTH
04  VISHAL

Select id, upper(replace(replace(replace(replace(replace(replace(name,'|',' '),chr(10),' '),chr(13),' '),chr(9),' '), chr(42), ' '), chr(40), ' ')) as NAME
from employee 

this will take out any ascii special characters and * But the result was:

ID   Name
01  RADHIKA N (MBA) MCA
02  VENU (BBA)
03  (MBA) ANANTH
04  VISHAL MCA

How do i remove "(MBA)" from the names?

For the sample data shown, the query below would work.

select trim(upper(regexp_replace(name,'[\(|\*](.*)[\)|\*]','')))
from tbl

If I understand correctly, you can use regexp_replace() . I would approach this in two steps:

select regexp_replace(regexp_replace(name, '[(][A-Z]+[)]', ''), '[^a-zA-Z ]', '')

The first removes the part in parentheses. The second keeps only alpha-numeric values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM