5

我有一个包含超过 90,000 条记录的表,其中一个字段是 phone_no。

我想替换 phone_no 列中的以下特殊字符。

"(",")","/"," ","-","+"

以下查询一次仅更新 1 个字符。

//SQL Query i have used to update 
UPDATE notary_info SET mobile_phone = REPLACE(mobile_phone, '/', '')

是否可以在一个 mysql 查询中替换所有上述特殊字符?

4

7 回答 7

8

尝试嵌套REPLACE()函数,如:

UPDATE notary_info SET mobile_phone = 
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(mobile_phone, '/', ''),'(',''),')',''),' ',''),'+',''),'-','');
于 2013-10-04T03:41:57.043 回答
4

不幸的是,MySQL 不允许您用一条语句同时替换多个字符。您可以链接REPLACE调用:

REPLACE(REPLACE(mobile_phone, "/", ""), "(", "")

听起来你想避免这种情况。在这种情况下,最好使用脚本语言来检查查询的每个结果并自行进行替换。许多语言可以简单地做到这一点,例如在 PHP 中:

strtr($row['mobile_phone'], array("()/ -+" => ""))
于 2013-10-04T03:44:06.473 回答
1
UPDATE notary_info SET mobile_phone = 
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(mobile_phone, '/', ''),'(',''),')',''),' ',''),'+',''),'-','');

样品小提琴

于 2013-10-04T03:40:43.963 回答
1

如果可以在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"

希望这对其他人有帮助

于 2015-06-10T18:27:07.260 回答
0

您将不得不链接REPLACE呼叫。例如:

UPDATE notary_info SET mobile_phone = REPLACE(REPLACE(mobile_phone, '/', ''), '+')
于 2013-10-04T03:40:55.960 回答
0

创建一个存储函数,该函数将使用其 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)

于 2015-07-02T05:50:16.153 回答
-1

替换下面的字符

   ~ ! @ # $ % ^ & * ( ) _ +
    ` - = 
    { } |
    [ ] \
    : " 
    ; '

    < > ?
    , . 

用这个 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
于 2015-02-11T11:03:29.413 回答