0

I tried a couple of things but it deleted the complete content from the column.

Here's what I want to do - A particular table [abc_tablename] in my MySQL database has a column [xyz_columnname] that has a lot of email IDs (more than 5000) and I want them to replace all the email IDs with a text [email ID removed]

I used the following query for SELECT:

SELECT * 
  FROM abc_tablename
 WHERE xyz_columnname LIKE '%@%'

How do I perform the replace function in this case? Can you help me with the query for the same. I tried this but it removed all the data from xyz_columnname

UPDATE abc_tablename 
   SET xyz_columnname = REPLACE (  xyz_columnname,  LIKE ''%@%'',  '**email ID removed**');

I'm using PHPMyAdmin to run these queries. Also, do note that xyz_columnname has a lot of text and numeric content along with the email ID.

4
  • Possible duplicate of SQL UPDATE with LIKE Commented Apr 27, 2017 at 22:01
  • Is the value in xyz_columnname just an email address and nothing else? Or is it an email address among a bunch of other text like blah blah [email protected] blah and you only want to replace the email in that text? If that is the case, you would likely need to use a regular expression to perform a replace. Mysql doesn't have a built in regexp replace function so you would need to use something like php and update it or define a udf version of regexp_replace (there are several out there). Commented Apr 27, 2017 at 22:48
  • @JonathanKuhn You mentioned it correct. The xyz_columnname has a lot of text and numeric content along with the email ID. Commented Apr 28, 2017 at 5:57
  • @ravichopra, well, like I said above, mysql doesn't have the capability out of the box to do pattern match and replace. You would either need to use something else like php and preg_replace to pull out all the rows where like %@%, fix them and update the rows one at a time or implement one of the many regexp_replace user defined functions within mysql use that. The latter would likely be more performant, but if you are doing this as a one off thing, just brute force it the easier way with php and in the future, fix it before insert. Commented Apr 28, 2017 at 17:04

1 Answer 1

1

You can simply use this command, this will update all rows which column xyz_columnname contains an @ to show email ID Removed

UPDATE abc_tablename 
    SET `xyz_columnname` = 'email ID removed'
    WHERE `xyz_columnname` LIKE '%@%'

Use ` (backquotes) for column name

Use ' or " for values

Don't use backticks with column values. use either single or double quotes otherwise mysql will consider that value as a column name.

Sign up to request clarification or add additional context in comments.

2 Comments

Will this work even if the xyz_columnname has has a lot of text and numeric content along with the email ID?
No, this will only work if xyz_columnname only contains email address. You didnt make that clear in your question that xyz_columnname contained multiple types of information. This is not best practice for SQL, you should use a separate column for each value.

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.