1
 UPDATE `link_tag`
 SET `TagID` = replace(TagID, 2, 13)

I'm specifically only trying to replace 2 with 13. When I use the above for example 202 becomes 13013 because it is replacing both instances of 2 within the number 202. I only want to replace just 2 with 13, not 2 within other larger numbers.

3
  • 1
    please show sample data Commented May 19, 2017 at 6:28
  • MySQL has no built in regex replacement support, and this could be very tricky. Commented May 19, 2017 at 6:29
  • What is the data type of TagID Commented May 19, 2017 at 6:36

1 Answer 1

1

If you specify a where clause, you'll only update the results that match the exact clause so you could try something like this:

UPDATE `link_tag`
SET `TagID` = replace(`TagID`, 2, 13)
WHERE `TagID` = 2

Now it'll only change the 2 where the TagID is actually equal to 2

A better way would be doing this:

UPDATE `link_tag` SET `TagID` = 13 WHERE `TagID` = 2
Sign up to request clarification or add additional context in comments.

3 Comments

@TimBiegeleisen Could you elaborate as to why not ?
@TimBiegeleisen That's the problem OP has though, he only wants to change a single "2" if that's the exact value of the column, he doesn't want a column "22" to become "1313"
@TimBiegeleisen It's not a problem, but I address the last sentence of the OP's post, where he explicitly states that this is his intention :)

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.