I need a query to remove all alphanumeric characters from a string and give me only special characters.
If string is '@#45gr@@3'
query should give me '@#@@'
.
I need a query to remove all alphanumeric characters from a string and give me only special characters.
If string is '@#45gr@@3'
query should give me '@#@@'
.
The old-fashioned way, with a replace()
call:
select translate(upper(txt)
, '.1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'
, '.') as spec_txt
from t42
/
With a replace()
solution is better to type all the characters we want to exclude. Yes that is the larger set (probably) but at least it's well-defined. We don't want to keep revising the code to handle new characters in the input string.
Obviously regex solution is more compact and who can deny the elegance of @venkatesh solution? However, evaluating regular expressions is more expensive than more focused SQL functions, so it's worth knowing alternative ways of doing things, for those cases when performance is an issue.
Everything written in comments is most probably very true (especially the 5th one that talks about exceptions, special cases etc.). My feeling is that Jörg knows more about regular expressions than I'll ever know.
Anyway, to cut a long story short, in its very simple appearance, regarding the question posted ("remove all numbers and letters"), something like this might work:
SQL> select regexp_replace('a@#45gr@@3$.', '[[:digit:]]+|[[:alpha:]]+', '') result
2 from dual;
RESULT
------
@#@@$.
SQL>