Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Special Characters from an Oracle String

From within an Oracle 11g database, using SQL, I need to remove the following sequence of special characters from a string, i.e.

~!@#$%^&*()_+=\{}[]:”;’<,>./?

If any of these characters exist within a string, except for these two characters, which I DO NOT want removed, i.e.: "|" and "-" then I would like them completely removed.

For example:

From: 'ABC(D E+FGH?/IJK LMN~OP' To: 'ABCD EFGHIJK LMNOP' after removal of special characters.

I have tried this small test which works for this sample, i.e:

select regexp_replace('abc+de)fg','\+|\)') from dual

but is there a better means of using my sequence of special characters above without doing this string pattern of '\+|\)' for every special character using Oracle SQL?

like image 984
tonyf Avatar asked Aug 15 '14 15:08

tonyf


People also ask

How do I remove special characters in SQL?

You can remove special characters from a database field using REPLACE() function. The special characters are double quotes (“ “), Number sign (#), dollar sign($), percent (%) etc.


1 Answers

You can replace anything other than letters and space with empty string

[^a-zA-Z ]

here is online demo


As per below comments

I still need to keep the following two special characters within my string, i.e. "|" and "-".

Just exclude more

[^a-zA-Z|-]

Note: hyphen - should be in the starting or ending or escaped like \- because it has special meaning in the Character class to define a range.

For more info read about Character Classes or Character Sets

like image 96
Braj Avatar answered Oct 12 '22 13:10

Braj