我有一个包含超过 90,000 条记录的表,其中一个字段是 phone_no。
我想替换 phone_no 列中的以下特殊字符。
"(",")","/"," ","-","+"
以下查询一次仅更新 1 个字符。
//SQL Query i have used to update
UPDATE notary_info SET mobile_phone = REPLACE(mobile_phone, '/', '')
是否可以在一个 mysql 查询中替换所有上述特殊字符?
我有一个包含超过 90,000 条记录的表,其中一个字段是 phone_no。
我想替换 phone_no 列中的以下特殊字符。
"(",")","/"," ","-","+"
以下查询一次仅更新 1 个字符。
//SQL Query i have used to update
UPDATE notary_info SET mobile_phone = REPLACE(mobile_phone, '/', '')
是否可以在一个 mysql 查询中替换所有上述特殊字符?
尝试嵌套REPLACE()
函数,如:
UPDATE notary_info SET mobile_phone =
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(mobile_phone, '/', ''),'(',''),')',''),' ',''),'+',''),'-','');
不幸的是,MySQL 不允许您用一条语句同时替换多个字符。您可以链接REPLACE
调用:
REPLACE(REPLACE(mobile_phone, "/", ""), "(", "")
听起来你想避免这种情况。在这种情况下,最好使用脚本语言来检查查询的每个结果并自行进行替换。许多语言可以简单地做到这一点,例如在 PHP 中:
strtr($row['mobile_phone'], array("()/ -+" => ""))
UPDATE notary_info SET mobile_phone =
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(mobile_phone, '/', ''),'(',''),')',''),' ',''),'+',''),'-','');
如果可以在mysql中创建函数:
DELIMITER $$
CREATE FUNCTION `replaceEx`(in_value varchar(4000), chars varchar(100), replace_char varchar(1)) RETURNS varchar(4000)
BEGIN
DECLARE res varchar(4000);
declare count int;
set res = in_value;
set count = char_length(chars);
WHILE (count > 0) DO
set res = replace(res,SUBSTRING(chars, count, 1),replace_char);
set count = count - 1;
END WHILE;
RETURN res;
END$$
DELIMITER ;
用法:
select replaceEx('mooxmyoolzand','xyz','');
返回"moomooland"
。
希望这对其他人有帮助
您将不得不链接REPLACE
呼叫。例如:
UPDATE notary_info SET mobile_phone = REPLACE(REPLACE(mobile_phone, '/', ''), '+')
创建一个存储函数,该函数将使用其 ASCII 代码去除特殊字符:
DROP FUNCTION IF EXISTS `cleanString`;
DELIMITER ;;
CREATE FUNCTION `cleanString`(`in_str` text) RETURNS text CHARSET utf8
BEGIN
/**
* Function will strip all non-ASCII and unwanted ASCII characters in string
*
* @author Sunny Attwal
*
* @param text in_arg
* @return text
*/
DECLARE out_str text DEFAULT '';
DECLARE c text DEFAULT '';
DECLARE pointer INT DEFAULT 1;
IF ISNULL(in_str) THEN
RETURN NULL;
ELSE
WHILE pointer <= LENGTH(in_str) DO
SET c = MID(in_str, pointer, 1);
IF (ASCII(c) NOT IN(33,34,35,36,37,38,39,40,41,42,43,44,63,126)) THEN
SET out_str = CONCAT(out_str, c);
ELSE
SET out_str = CONCAT(out_str, ' ');
END IF;
SET pointer = pointer + 1;
END WHILE;
END IF;
RETURN out_str;
END
;;
DELIMITER ;
注意:在 NOT IN 条件中添加空间字符 ascii 代码,供参考在这里http://www.ascii.cl/htmlcodes.htm查找空间字符 ascii 代码
运行 sql 查询为: UPDATE notary_info SET mobile_phone = cleanString(mobile_phone)
替换下面的字符
~ ! @ # $ % ^ & * ( ) _ +
` - =
{ } |
[ ] \
: "
; '
< > ?
, .
用这个 SQL
选择笔记作为note_original,
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(note, '\"', ''),
'.', ''),
'?', ''),
'`', ''),
'<', ''),
'=', ''),
'{', ''),
'}', ''),
'[', ''),
']', ''),
'|', ''),
'\'', ''),
':', ''),
';', ''),
'~', ''),
'!', ''),
'@', ''),
'#', ''),
'$', ''),
'%', ''),
'^', ''),
'&', ''),
'*', ''),
'_', ''),
'+', ''),
',', ''),
'/', ''),
'(', ''),
')', ''),
'-', ''),
'>', ''),
' ', '-'),
'--', '-') as note_changed FROM invheader