Link to home
Start Free TrialLog in
Avatar of jeaney
jeaney

asked on

SQL REPLACE for multiple characters

Hi

I want to remove multiple characters (that are not necessary sequential) in a string.

To remove the character 'a' from a string I use the following:

REPLACE(mystring, 'a', '')

But if I want to remove all 'a's 'b's and 'c's how do I do it?
The following does not work but displays the logic I need:

REPLACE(mystring, 'a' | 'b' | 'c', '')

Fast reponse greatly appreciated!
Avatar of chedgey
chedgey
Flag of United Kingdom of Great Britain and Northern Ireland image

Messy, but how about:

Replace( Repalce( Replace( MyString,'a','' ),'b','' ),'c','' )

Regards

Chedgey
Avatar of jeaney
jeaney

ASKER

Mmmmh, thanks chedgey. I did think about doing it this way but was hoping there was a better way :(

Do you think this is the only way it can be done?

Will hold out to see if anyone else has any other suggestions.
Try TRANSLATE:

Select translate('AaBbCcUYTUYTUYT','~abc','~') From Dual;

TRANSLATE('A
------------
ABCUYTUYTUYT

PS: Note the required '~' character (or any other you want).

Avatar of jeaney

ASKER

Thanks Mike but I'm being told that TRANSLATE is not a recognised function name. I'm using MSSQl
SOLUTION
Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial