gpt4 book ai didi

mysql - mysql 查询中的正则表达式

转载 作者:行者123 更新时间:2023-11-29 00:58:14 25 4
gpt4 key购买 nike

我有一个以 address 作为列的表。

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

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

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

谢谢。

最佳答案

也许这个function帮助你

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 - mysql 查询中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4990530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com