2013-10-04 41 views
4

我有一個包含多於90,000條記錄的表,其中一個字段是phone_no。如何從mysql中的一列表中替換多個字符

我想從phone_no列中替換下列特殊字符。

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

以下查詢一次只更新一個字符。

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

是否有可能在一個mysql查詢中替換上述所有特殊字符?

回答

1
UPDATE notary_info SET mobile_phone = 
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(mobile_phone, '/', ''),'(',''),')',''),' ',''),'+',''),'-',''); 

SAMPLE FIDDLE

0

你將不得不鏈REPLACE電話。例如:

UPDATE notary_info SET mobile_phone = REPLACE(REPLACE(mobile_phone, '/', ''), '+') 
7

儘量嵌套REPLACE()功能,如:

UPDATE notary_info SET mobile_phone = 
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(mobile_phone, '/', ''),'(',''),')',''),' ',''),'+',''),'-',''); 
+0

這種方式對我來說非常完美,它完全符合我的預期,謝謝。 –

4

不幸的是,MySQL不會讓你用一個語句同時替換多個字符。你可以連續通話REPLACE

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

這聽起來像你正試圖避免,雖然。在這種情況下,最好使用腳本語言來查看每個查詢結果並自己進行替換。許多語言可以在PHP中簡單地做到這一點,例如:

​​
-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 
0

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

希望這有助於別人

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 ; 

注意:添加空間字符的ASCII碼內NOT IN條件,以供參考找到空間這裏炭ascii碼http://www.ascii.cl/htmlcodes.htm

運行SQL查詢爲: UPDATE SET notary_info = MOBILE_PHONE cleanString(MOBILE_PHONE)

相關問題