I have a results set from a webform that includes a phone number for each set. the format of this phone number is not enforced (some are xxxxxxxxxx, some are (xxx)xxx-xxxx and some are xxx-xxx-xxxx). It was short sighted, and now I need to be able get a result based on the phone number (Views exposed filter).
The best way for me to solve this is to reformat the values in this field with an sql query so that they are stripped of any non-numeric values. I've tried a couple of functions I've found on similar questions, and none seem to be working (I'm using mysql workbench and getting a "function does not exist" error). This is something I'm doing once and am looking for a query I can run that will strip all non-numeric values. I'll only need to run it once, because I am validating phone numbers to be numeric only from here on.
Is there a sql query that will do what I need? With PHP it would simply be
update table set data = preg_replace("/[^0-9]/", "", data) where condition
But I can't seem to find a way to do this with SQL.
There isn't any "builtin" function that will do this operation in MySQL.
One option is to create your own stored function (if you have sufficient privileges on the database).
And be sure to test this before you use it an UPDATE statement.
Returns:
(The last two rows might give us pause to reconsider what we really want to achieve. If it were me, I would creating a new column, and saving the existing value in it, before I did an UPDATE.)
This may work but little tedious:
Get the list of number from the table
On each value
Then do a regex pattern and update that value to the ID
On MySQL 8.0+ there is a new native function called REGEXP_REPLACE. A clean solution to this question would be:
It's not pretty, but...
You may have to duplicate the concatenated IF function more times depending on the length of field and how much formatting was entered.
See the query in action at SQLFiddle