Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 删除特殊字符的Sql查询_Mysql - Fatal编程技术网

Mysql 删除特殊字符的Sql查询

Mysql 删除特殊字符的Sql查询,mysql,Mysql,我在mysql表中有一个带有特殊字符的varchar字段。我想使用MYSQL查询删除该字符串中的所有特殊字符。使用 update table_name set field_name = REPLACE(field_name,'/','') MySQL不支持这样的查询。您在MySQL查询中的最佳表现是: update TABLENAME set FIELDNAME = replace(FIELDNAME,'_','') 替换以下字符 ~ ! @ # $ % ^ & * ( ) _ +

我在mysql表中有一个带有特殊字符的
varchar
字段。我想使用MYSQL查询删除该字符串中的所有特殊字符。

使用

update table_name set field_name = REPLACE(field_name,'/','')

MySQL不支持这样的查询。您在MySQL查询中的最佳表现是:

update TABLENAME
set FIELDNAME = replace(FIELDNAME,'_','')

替换以下字符

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

< > ?
, . 
~!@$%^&*( ) _ +
` - = 
{ } |
[ ] \
: " 
; '
< > ?
, . 
使用此SQL

SELECT note as 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
选择注释作为原始注释,
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(
替换(注“,”),
'.', ''),
'?', ''),
'`', ''),

“您可以使用下面的函数

DROP FUNCTION IF EXISTS `stripSpeciaChars`;
DELIMITER ;;CREATE FUNCTION `stripSpeciaChars`(`dirty_string` varchar(2048),allow_space TINYINT,allow_number TINYINT,allow_alphabets TINYINT,no_trim TINYINT) RETURNS varchar(2048) CHARSET utf8
BEGIN
/**
 * MySQL function to remove Special characters, Non-ASCII,hidden characters leads to spaces, accents etc
 * Downloaded from http://DevZone.co.in
 * @param VARCHAR dirty_string : dirty string as input
 * @param VARCHAR allow_space : allow spaces between string in output; takes 0-1 as parameter
 * @param VARCHAR allow_number : allow numbers in output; takes 0-1 as parameter
 * @param VARCHAR allow_alphabets : allow alphabets in output; takes 0-1 as parameter
 * @param VARCHAR no_trim : don't trim the output; takes 0-1 as parameter
 * @return VARCHAR clean_string : clean string as output
 * 
 * Usage Syntax: stripSpeciaChars(string,allow_space,allow_number,allow_alphabets,no_trim);
 * Usage SQL> SELECT stripSpeciaChars("sdfa7987*&^&*ÂÃ ÄÅÆÇÈÉÊ sd sdfgËÌÍÎÏÑ ÒÓÔÕÖØÙÚàáâã sdkarkhru",0,0,1,0);
 * result : sdfasdsdfgsdkarkhru
 */
      DECLARE clean_string VARCHAR(2048) DEFAULT '';
      DECLARE c VARCHAR(2048) DEFAULT '';
      DECLARE counter INT DEFAULT 1;

      DECLARE has_space TINYINT DEFAULT 0; -- let spaces in result string
      DECLARE chk_cse TINYINT DEFAULT 0; 
      DECLARE adv_trim TINYINT DEFAULT 1; -- trim extra spaces along with hidden characters, new line characters etc.     

         if allow_number=0 and allow_alphabets=0 then
        RETURN NULL;
      elseif allow_number=1 and allow_alphabets=0 then
      set chk_cse =1;
     elseif allow_number=0 and allow_alphabets=1 then
      set chk_cse =2;
      end if;     

      if allow_space=1 then
      set has_space =1;
      end if;

       if no_trim=1 then
      set adv_trim =0;
      end if;

      IF ISNULL(dirty_string) THEN
            RETURN NULL;
      ELSE

      CASE chk_cse
      WHEN 1 THEN 
      -- return only Numbers in result
      WHILE counter <= LENGTH(dirty_string) DO

                  SET c = MID(dirty_string, counter, 1);

                  IF ASCII(c) = 32 OR ASCII(c) >= 48 AND ASCII(c) <= 57  THEN
                        SET clean_string = CONCAT(clean_string, c);
                  END IF;

                  SET counter = counter + 1;
            END WHILE;
      WHEN 2 THEN 
      -- return only Alphabets in result
      WHILE counter <= LENGTH(dirty_string) DO

                  SET c = MID(dirty_string, counter, 1);

                  IF ASCII(c) = 32 OR ASCII(c) >= 65 AND ASCII(c) <= 90  OR ASCII(c) >= 97 AND ASCII(c) <= 122 THEN
                        SET clean_string = CONCAT(clean_string, c);
                  END IF;

                  SET counter = counter + 1;
            END WHILE;
      ELSE
       -- return numbers and Alphabets in result
        WHILE counter <= LENGTH(dirty_string) DO

                  SET c = MID(dirty_string, counter, 1);

                  IF ASCII(c) = 32 OR ASCII(c) >= 48 AND ASCII(c) <= 57 OR ASCII(c) >= 65 AND ASCII(c) <= 90  OR ASCII(c) >= 97 AND ASCII(c) <= 122 THEN
                        SET clean_string = CONCAT(clean_string, c);
                  END IF;

                  SET counter = counter + 1;
            END WHILE;      
    END CASE;            
      END IF;


      -- remove spaces from result
      if has_space=0 then
      SET clean_string =REPLACE(clean_string,' ','');
      end if;

       -- remove extra spaces, newline,tabs. from result
     if adv_trim=1 then
      SET clean_string =TRIM(Replace(Replace(Replace(clean_string,'\t',''),'\n',''),'\r',''));
      end if;    

      RETURN clean_string;
END
;;
DELIMITER ;SELECT stripSpeciaChars("foo79ÒÓÔÕÖØÙÚàáâã 87*&bÄÅÆÇÈÉÊar",1,0,1,1); -- result foo bar
DROP函数(如果存在)`stripSpecialChars`;
分隔符;创建函数'stripSpecialChars'('dirty_string`varchar(2048),allow_space TINYINT,allow_number TINYINT,allow_alphabets TINYINT,no_trim TINYINT)返回varchar(2048)字符集utf8
开始
/**
*MySQL函数用于删除特殊字符、非ASCII、隐藏字符、空格、重音符号等
*下载自http://DevZone.co.in
*@param VARCHAR dirty_string:作为输入的脏字符串
*@param VARCHAR allow_space:允许输出中字符串之间有空格;参数为0-1
*@param VARCHAR allow_number:允许输出中的数字;参数为0-1
*@param VARCHAR allow_alphabets:allow alphabets in output;取0-1作为参数
*@param VARCHAR no_trim:不修剪输出;将0-1作为参数
*@return VARCHAR clean_string:clean string作为输出
* 
*用法语法:stripSpecialChars(字符串、允许\u空格、允许\u数字、允许\u字母、不允许\u修剪);
*用法SQL>SELECT StripSpecialChars(“sdfa7987*&&&*sdfg*&*SDKhru*,”0,0,1,0);
*结果:sdfasdsdfgsdkarkhru
*/
声明clean_string VARCHAR(2048)为默认值“”;
声明c VARCHAR(2048)为默认值“”;
声明计数器INT默认值为1;
DECLARE的\u space TINYINT默认值为0;--让空格出现在结果字符串中
声明chk_cse TINYINT默认值为0;
声明adv_trim TINYINT DEFAULT 1;--修剪额外的空格以及隐藏字符、新行字符等。
如果allow_number=0且allow_alphabets=0,则
返回NULL;
elseif allow_number=1,allow_alphabets=0,则
设置chk_cse=1;
elseif allow_number=0,allow_alphabets=1,则
设置chk_cse=2;
如果结束;
如果allow_space=1,则
集合具有_空间=1;
如果结束;
如果没有_trim=1,则
设置adv_trim=0;
如果结束;
如果为空(脏字符串),则
返回NULL;
其他的
案例chk_cse
当我
--只返回结果中的数字

当计数器=48和ASCII(c)时,这些特殊字符是什么?特殊字符,如“\u1”,““-”,“\”等,我也要删除它们spaces@ramkumar我认为你需要比“等”更具体一点。也许更容易判断要保留哪些字符?我只希望字符串中有字母和数字,并删除所有其他字符。@ramkumar,你找到答案了吗?