3

In not a database guy but: I have mixed up data in a mySql database that I inherited.

Some Phone numbers are formatted (512) 555-1212 (call it dirty) Others 5125551212 (Call it clean)

I need a sqlstamet that says

UPDATE table_name
SET Phone="clean'(Some sort of cleaning code  -  regex?)
WHERE Phone='Dirty'
Spudley
  • 166,037
  • 39
  • 233
  • 307
JVMX
  • 1,016
  • 2
  • 12
  • 23

2 Answers2

8

Unfortunately there's no regex replace/update in MySQL. If it's just parentheses and dashes and spaces then some nested REPLACE calls will do the trick:

UPDATE table_name
SET Phone = REPLACE(REPLACE(REPLACE(REPLACE(Phone, '-', ''), ')', ''), '(', ''), ' ', '')
Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69
0

To my knowledge you can't run a regexp to replace data during the update process. Only during the SELECT statement.

Your best bet is to use a scripting language that you're familiar with and read the table and change it that way. Basically by retrieving all the entries. Then using a string replace to match a simple regexp such as [^\d]* and remove those characters. Then update the table with the new value.

Also, see this answer: How to do a regular expression replace in MySQL?

Community
  • 1
  • 1
CP510
  • 2,297
  • 15
  • 15