2011-05-20 102 views
8

我有一個包含數千條記錄的數據庫,並且需要刪除其中一個字段以確保它只包含某些字符(字母數字,空格和單引號)。我可以使用什麼SQL去除整個數據庫中該字段的任何其他字符(例如斜槓等)?刪除數據庫字段中的特殊字符

+0

只是做了一些更新和我們e取代。幾千條記錄不是那麼多......男人! – Hogan 2011-05-20 02:57:30

回答

20
update mytable 
set FieldName = REPLACE(FieldName,'/','') 

這是一個很好的開始。

+0

這是做到了。我更喜歡Rasika提到的正則表達式,但是這個網站位於共享服務器上。 – MarathonStudios 2011-05-20 08:55:18

1

看看LIB_MYSQLUDF_PREG需要編譯到MySQL服務器,但有先進的正則表達式設施,如preg_replace這將有助於您的任務。

5

Replace()函數是首選。但是,特殊字符在控制檯中寫入有時會很棘手。對於那些你可以結合使用替換和Char()函數。

例如去除€

Update products set description = replace(description, char(128), ''); 

你可以找到所有的Ascii values here

理想情況下,你可以做一個正則表達式查找所有的特殊字符,但顯然that's not possible with MySQL.

除此之外,你需要通過運行它你最喜歡的腳本語言。

3

這也可能有用。

首先你必須知道數據庫和/或表的字符集。例如,讓我們假設您有一個UTF-8環境,並且您想從一個字段中去除/去除符號(如圈出的註冊符號,圈出的版權符號和註冊商標符號),然後通過bing或yahoo或google搜索互聯網在UTF-8系統中的這些符號的十六進制代碼值:

 
Symbol      Utf-8 Hex 
=======      ========= 
circled copyright    C2A9 
circled registered    C2AE 
Trademark (i.e., TM)   E284A2 

然後你擦洗從表t1場F1選擇SQL,使用六角/ UNHEX設施與替換功能相結合,將最有可能的樣子這樣的:

SELECT cast(unhex(replace(replace(replace(hex(f1),'C2A9',''),'C2AE',''),'E284A2','')) AS char) AS cleanf1 FROM t1 ;

以上,請注意原始字段被清理/清理的是f1,表格是t1,輸出標題是cleanf1。 「as char」鑄造是必要的,因爲不帶它,我測試的mysql 5.5.8正在返回blob。希望這有助於

1

在闡述Vinnies答案...你可以使用下面的(注意在最後兩個語句逃逸......

update table set column = REPLACE(column,"`",""); 
update table set column = REPLACE(column,"~",""); 
update table set column = REPLACE(column,"!",""); 
update table set column = REPLACE(column,"@",""); 
update table set column = REPLACE(column,"#",""); 
update table set column = REPLACE(column,"$",""); 
update table set column = REPLACE(column,"%",""); 
update table set column = REPLACE(column,"^",""); 
update table set column = REPLACE(column,"&",""); 
update table set column = REPLACE(column,"*",""); 
update table set column = REPLACE(column,"(",""); 
update table set column = REPLACE(column,")",""); 
update table set column = REPLACE(column,"-",""); 
update table set column = REPLACE(column,"_",""); 
update table set column = REPLACE(column,"=",""); 
update table set column = REPLACE(column,"+",""); 
update table set column = REPLACE(column,"{",""); 
update table set column = REPLACE(column,"}",""); 
update table set column = REPLACE(column,"[",""); 
update table set column = REPLACE(column,"]",""); 
update table set column = REPLACE(column,"|",""); 
update table set column = REPLACE(column,";",""); 
update table set column = REPLACE(column,":",""); 
update table set column = REPLACE(column,"'",""); 
update table set column = REPLACE(column,"<",""); 
update table set column = REPLACE(column,",",""); 
update table set column = REPLACE(column,">",""); 
update table set column = REPLACE(column,".",""); 
update table set column = REPLACE(column,"/",""); 
update table set column = REPLACE(column,"?",""); 
update table set column = REPLACE(column,"\\",""); 
update table set column = REPLACE(column,"\"",""); 
1

我已經創建了簡單的功能這個

DROP FUNCTION IF EXISTS `regex_replace`$$ 

CREATE FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000)) RETURNS VARCHAR(1000) CHARSET utf8mb4 
    DETERMINISTIC 
BEGIN  
    DECLARE temp VARCHAR(1000); 
    DECLARE ch VARCHAR(1); 
    DECLARE i INT; 
    SET i = 1; 
    SET temp = ''; 
    IF original REGEXP pattern THEN 
     loop_label: LOOP 
      IF i>CHAR_LENGTH(original) THEN 
       LEAVE loop_label; 
      END IF; 

      SET ch = SUBSTRING(original,i,1); 

      IF NOT ch REGEXP pattern THEN 
       SET temp = CONCAT(temp,ch); 
      ELSE 
       SET temp = CONCAT(temp,replacement); 
      END IF; 

      SET i=i+1; 
     END LOOP; 
    ELSE 
     SET temp = original; 
    END IF; 

    RETURN temp; 
END 

用例:

SELECT <field-name> AS NormalText, regex_replace('[^A-Za-z0-9 ]', '', <field-name>)AS RegexText FROM 
<table-name>