删除数据库字段中的特殊字符
我有一个包含数千条记录的数据库,并且需要删除其中一个字段以确保它只包含某些字符(字母数字,空格和单引号)。我可以使用什么SQL去除整个数据库中该字段的任何其他字符(例如斜杠等)?删除数据库字段中的特殊字符
update mytable
set FieldName = REPLACE(FieldName,'/','')
这是一个很好的开始。
这是做到了。我更喜欢Rasika提到的正则表达式,但是这个网站位于共享服务器上。 – MarathonStudios 2011-05-20 08:55:18
Replace()函数是首选。但是,特殊字符在控制台中写入有时会很棘手。对于那些你可以结合使用替换和Char()函数。
例如去除€
Update products set description = replace(description, char(128), '');
你可以找到所有的Ascii values here
理想情况下,你可以做一个正则表达式查找所有的特殊字符,但显然that's not possible with MySQL.
除此之外,你需要通过运行它你最喜欢的脚本语言。
这也可能有用。
首先你必须知道数据库和/或表的字符集。例如,让我们假设您有一个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。希望这有助于
在阐述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,"\"","");
我已经创建了简单的功能这个
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>
只是做了一些更新和我们e取代。几千条记录不是那么多......男人! – Hogan 2011-05-20 02:57:30