Nikhil Agrawal Nikhil Agrawal - 7 months ago 15
SQL Question

Replace multiple characters in SQL

I have a problem where I want to replace characters

I am using

replace
function but that is not giving desired output.

Values of column table_value needs to replaced with their fill names like

E - Email

P - Phone

M - Meeting

enter image description here

I am using this query

select table_value,
replace(replace(replace(table_value, 'M', 'MEETING'), 'E', 'EMAIL'), 'P', 'PHONE') required_value
from foobar


so second
required_value
row should be
EMAIL,PHONE,MEETING
and so on.

What should I do so that required value is correct?

Answer

The below will work (even it's not a smart solution).

select 
    table_value, 
    replace(replace(replace(replace(table_value, 'M', 'MXXTING'), 'E', 'XMAIL'), 'P', 'PHONX'), 'X', 'E') required_value 
from foobar
Comments