I'm working on MySQL and having column phone-number. And trying to use regex for this and not succeed.
How can I remove all special character from this column ?
phone-number
'8-903-400-65-38'
'+79265682388'
'8.10492E+15'
'8-913-469-38-35'
'+79882856253'
'+79110987703'
'+7 (495) 989-21-16'
'8142 77-55-51'
'+79378299427'
Please can anyone help me on this issue? I don't want to lose these list of contact numbers.
Thanks in advance
The canned answer here is just to chain together a series of calls to MySQL's REPLACE function:
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone-number, '-', ''), '+', ''), '.', ''), '(', ''), ')', ''), ' ', '')
This would remove the following characters from the phone-number column:
- + . ( ) and space
The nicer solution would be to use a regular expression to do the replacement, but alas MySQL does not any such built in support for regex replace.
If you don't want a fugly REPLACE chain, then you could write some dynamic MySQL code which iterates over a set of characters which you define, and does a number of updates to the table. Here is what one such update would look like:
UPDATE yourTable
SET phone-number = REPLACE(phone-number, '+', '')
You could perform one update for each character and handle it this way.
Related
I'm trying to remove parenthesis from the area code of a number. I'm able to do so but when I try to concatenate the two Replace functions, the numbers repeat with only one parenthesis removed.
This is what I tried so far:
SELECT HomePhone, REPLACE(HomePhone, '(', '') +
REPLACE(HomePhone, ')', '') AS Expr1
FROM dbo.Employees
http://i.imgur.com/4iJoFzE.png
Nest don't add
Replace(Replace(HomePhone,')',''),'(','')
Look at how the function replace works. It expects string With Text To Evaluate, string to replace, string to replace with)
By adding them you should be getting the number listed twice, but if the data type isn't long enough it may be truncating values. by nesting you're telling the system to replace the ) and then use that string w/o the ) to have the ( replaced with ''.
You cannot concatenate in this way, you must use the concat function in SQL. Or use thus:
SELECT HomePhone, REPLACE(REPLACE(HomePhone, ')', ''), '(', '') AS Expr1 FROM dbo.Employees
I'm just getting used to MySQL, I've come from a SQL Server background...
This SQL query builds an address how it should in SQL Server, how can I adapt it to use within MySQL. When I run it in SQL Server it displays all the data within each field, when run in MySQL it just shows me the first field.
Why would this be, what should I do different in MySQL?
SELECT COALESCE(House, '') + ' ' + COALESCE(StreetName, '') + ' ' + COALESCE(TownCity, '') + ' ' + COALESCE(Postcode, '') AS Display
FROM MyTable
WHERE Postcode LIKE '%A1 2AB%'
Use the concat() function:
SELECT concat(COALESCE(House, ''), ' ', COALESCE(StreetName, ''), ' ',
COALESCE(TownCity, ''), ' ', COALESCE(Postcode, '')
) AS Display
FROM MyTable
WHERE Postcode LIKE '%A1 2AB%';
You can also do this with concat_ws(). This eliminates the need for all the spaces:
SELECT concat_ws(COALESCE(House, ''), COALESCE(StreetName, ''),
COALESCE(TownCity, ''), COALESCE(Postcode, '')
) AS Display
FROM MyTable
WHERE Postcode LIKE '%A1 2AB%';
What happens in MySQL is that the + does just what you expect: it adds numbers. A string that contains a number is converted to a number automatically, with silent errors for strings that have no numbers. In practice, this means that a string that starts with a non-digit (and non-decimal point) is converted to a 0.
So, house, which presumably usually numeric, is converted to a number just fine. All the other strings are converted to numbers but become zero and the house number is not changed. You would have gotten much different results if your post codes were American-style zip codes (which are typically numeric).
EDIT:
As #fthiella points out, the coalesce() is not necessary for concat_ws(). The two statements would do different things, because NULLs in the original query result in repeated separators. NULLs in the concat_ws() version would have only a single separator (which might be desirable).
However, I would tend to keep the coalesce() anyway. The behavior of concat() and concat_ws() varies in this regard. concat() returns NULL if any of its arguments is NULL. concat_ws() skips NULL arguments after the initial separator. Who can remember that distinction? It sounds like a recipe for confusion in production code. So, I would also use coalesce() even though it is optional.
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'
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, '-', ''), ')', ''), '(', ''), ' ', '')
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?
I have trouble to remove multiple unwanted sign in MySql database as too huge.
Is there any availabe script to remove that sign?
sample Record With multiple sign :-
[]No. 11, Persiaran Bukit [] Satu&[]Taman #Sri %Nibong
use REPLACE function.
UPDATE tablename
SET ColName = REPLACE(REPLACE(REPLACE(Colname, '&', ''), '#', ''), '%', '')
What I suggest here is prepare a table that contains all the possible unwanted characters and then process them in one select query using replace function.
I am using MySQL. My data has a column called text, which uses the TEXT data type.
There are several newlines for each record in this column. I want to remove all new lines with a sql query. How can I do that?
Try this one -
CREATE TABLE table1(column1 TEXT);
INSERT INTO table1 VALUES ('text1\r\ntext2
text3');
SELECT * FROM table1;
--------
text1
text2
text3
UPDATE table1 SET column1 = REPLACE(column1, '\r\n', '');
SELECT * FROM table1;
--------
text1text2text3
The previous suggestions did not work for me. It only seems to work if I had actually typed the \r and \n text in as text. I found the following to work well -
replace(replace([MyFieldName],char(13),''),char(10),'')
I also created a calculated field in my table which uses this formula. That way I can just reference that field in areas of my program that were breaking with the original field contents.
Given suggestion i.e. REPLACE(REPLACE(DBField, '\n', ''), '\r', '') won't work if there are invisible html code like \n \r. For that you have to use char code.
Example:
REPLACE(REPLACE(REPLACE(DBField, CHAR(10), ''), CHAR(13), ''), CHAR(9), '')
Try this
REPLACE(REPLACE(FIELD, '\n', ''), '\r', '')
You can use REPLACE(text,'\n\r',' ') for it.
i've tried all said here but neither worked for me, my DB is IBM Informix,
however i managed to solve the problem like this:
UPDATE personas SET foto_path = SUBSTRING(foto_path FROM 1 FOR LENGTH(foto_path) - 1);
Hope it helps other in similar situation.
The above version with single backslash cleared up some for me but also had to run this to clear up the rest.
REPLACE(REPLACE(FIELD, '\\n', ''), '\\r', '')