0
SELECT id FROM people WHERE id REGEXP 'id-(\d)*';

result

id-1
id-2
id-3

This above query works but when i run update query below, it don't affect any rows

UPDATE people SET id = REPLACE(id, 'id-(\d)*', 'id-4');
0 row(s) affected

Somebody can explain for me, what wrong ?

5
  • MySQL's regex engine does not do replacements or capture groups. It's purely for matching in a WHERE clause. Commented Dec 20, 2011 at 4:24
  • Which database you are using? Commented Dec 20, 2011 at 4:59
  • You would be better off if you re-did the DB structure. This seems like madness. Commented Dec 20, 2011 at 5:18
  • @SomnathMuluk : i'm using Mysql Commented Dec 20, 2011 at 11:05
  • @tereško : it is only a example. i want to update the value in a field and it very good for me if i can use something like Regexp in mysql Commented Dec 20, 2011 at 11:07

3 Answers 3

1

REPLACE() doesn't search for regular expressions. Try this. But regexp_replace() function is supported by some of the databases.

This works for oracle, PostgreSQL. It doesn't work for MySQL.

 UPDATE people  SET id  = regexp_replace(id ,'id-(\d)*' , 'id-4');
Sign up to request clarification or add additional context in comments.

5 Comments

the question is tagged to mysql, why would you suggest something that is irrelevant ?
I specifically written test cases for different databases. You may see I have written comment in question section for asking database name. I thought this answer might help for thinking. Should I delete answer?
well, now the OP has tagged to mysql, then your answer is become irrelevant
@all : thanks for sharing , now i know about the regexp_replace with oracle and PostgreSQL. Do not need to delete your answer ^^!
@all This answer is definitively usefull, in the update can be processed offline user can unload / load into oracle or postgresql / unload / load back into mysql
0

REPLACE does not use regular expressions, only vanilla strings. You're searching for the literal string 'id-(\d)*', not the 'id-' followed by any number of digits. Presumably, you do not have such a weird string in your database.

Comments

0

REPLACE() does text replacement. There is no regular expression replacement in MySQL. Its better you perform REGEX with some server side scripting.

1 Comment

What i should use instead REPLACE in this case ?

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.