mysql 查询中的正则表达式 [英] REGEX in mysql query

查看:46
本文介绍了mysql 查询中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以 address 为列的表格.

i have a table with address as column.

地址的值为 "#12-3/98 avenue street",其中包含数字、特殊字符和字母.

values for address is "#12-3/98 avenue street", which has numbers, special characters and alphabets.

我想用 regex 编写我的 sql 查询以从地址值中删除特殊字符

i want to write my sql query usng regex to remove special characters from the address value

例如:12398avenuestreet"将是删除特殊字符后我想要的值

ex: "12398avenuestreet" will be the value i want after removing the special characters

谢谢.

推荐答案

也许这个 功能帮你

CREATE FUNCTION strip_non_alpha(
_dirty_string varchar(40)
)
RETURNS varchar(40)
BEGIN
DECLARE _length int;
DECLARE _position int;
DECLARE _current_char varchar(1);
DECLARE _clean_string varchar(40);

SET _clean_string = '';
SET _length = LENGTH(_dirty_string);
SET _position = 1;
WHILE _position <= _length DO
SET _current_char = SUBSTRING(_dirty_string, _position, 1);

IF _current_char REGEXP '[A-Za-z0-9]' THEN
SET _clean_string = CONCAT(_clean_string, _current_char);
END IF;

SET _position = _position + 1;
END WHILE;

RETURN CONCAT('', _clean_string);
END;

所以你需要这样称呼它

update mytable set address = strip_non_alpha(address);

这篇关于mysql 查询中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆