0

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??

2 Answers 2

8

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

UPDATE yourTable SET yourColumn = '456' WHERE yourColumn = '123';
Sign up to request clarification or add additional context in comments.

4 Comments

And a +1 to you for being the basis of my answer :p
As usual, I was seeking an overly-complex solution to a simple task. Thank you!
@FurryWombat Hey, that's my bit :p
Hahaha, beat you to the punch. Gotta start working on that... life would be so much easier.
4

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.

2 Comments

+1 Good point about other columns possibly needing to be updated.
Duly noted! For this situation, I will not need this, but will definitely keep in mind for future reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.