Home > OS >  Replace two String/Data using only one Query
Replace two String/Data using only one Query

Time:04-22

I'm trying to replace the two strings and change it to '-'.

 -------- -------- -------- -------- -------- 
| field1 | field2 | field3 | field4 | field5 |
 -------- -------- -------- -------- -------- 
| K      | V      |      1 | T      |        |
| O      | G      |      2 | O      | D      |
| B      |        |      3 | S      | A      |
| K      | H      |      4 | X      | Z      |
|        | V      |      5 |        | B      |
 -------- -------- -------- -------- -------- 

Under field5, I want to replace 'A' and 'B' to '-' but trying this code doesn't work:

SELECT REPLACE(REPLACE(field5,'A', '-'), 'B', '-')),
    -> FROM tblPrelim;

Is there a way that I can replace it with only one query? Any help would be greatly appreciated.

CodePudding user response:

You need to use UPDATE to modify the table.

UPDATE tblPrelim
SET field5 = REPLACE(REPLACE(field5,'A', '-'), 'B', '-')

If the field always contains just a single character, so you're replacing the entire value rather than just a substring, you can simplify this to:

UPDATE tblPrelim
SET field5 = '-'
WHERE field5 IN ('A', 'B')
  • Related