Question

Here's the issue. My query:

UPDATE `table` SET `column` = replace(`column`,'123','456');

Is not going to work. Here's why:

The numeric string '4123' now becomes '4456'.

What I would like is for only EXACT matches (column value equals exactly '123') to be replaced. Is this not possible with a simple UPDATE SET = REPLACE() query??

Was it helpful?

Solution

Seems like you shouldn't need REPLACE to do this:

UPDATE yourTable SET yourColumn = '456' WHERE yourColumn = '123';

OTHER TIPS

As Paul says, you shouldn't need a REPLACE() call, just add a suitable WHERE clause.

That said, if this is part of a bigger query updating other columns too, try this:
SET `column`=IF(`column`=123, 456, `column`)

This will only give that column a new value if it meets the condition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top