mysql set varchar_mySQL set a varchar without the special characters

Method 1:

You can use the REPLACE() method to remove special characters in mysql, don't know if it's very efficient though. But it should work.

Like Below:

SELECT Replace(Replace(product_name,'@',''),'+','') as prod_type

From Table1

Method 2:

If you have All other Special Charcter then go with this (Source)

-- ----------------------------

-- Function structure for `udf_cleanString`

-- ----------------------------

DROP FUNCTION IF EXISTS `udf_cleanString`;

DELIMITER ;;

CREATE FUNCTION `udf_cleanString`(`in_str` varchar(4096)) RETURNS varchar(4096) CHARSET utf8

BEGIN

DECLARE out_str VARCHAR(4096) DEFAULT '';

DECLARE c VARCHAR(4096) 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) > 31 AND ASCII(c) < 127 THEN

SET out_str = CONCAT(out_str, c);

END IF;

SET pointer = pointer + 1;

END WHILE;

END IF;

RETURN out_str;

END

;;

DELIMITER ;

After that just call the function as follows:

SELECT product_name, udf_cleanString(product_name) AS 'product_Type'

FROM table1;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值