我有一个以address为列的表。
地址的值是"#12-3/98 avenue street",它有数字、特殊字符和字母。
我希望使用regex编写sql查询,以从地址值中删除特殊字符。
示例:"12398avenuestreet“将是删除特殊字符后所需的值。
谢谢。
发布于 2011-02-14 09:42:40
也许这个函数能帮你
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);发布于 2011-02-14 09:35:21
您不需要RegExp来替换简单的字符。
MySQL字符串函数
发布于 2011-02-14 09:37:21
不幸的是,MySQL正则表达式“只匹配”,您不能在查询中进行替换。这只给你留下了这样的东西(女巫是非常非常愚蠢的):
SELECT REPLACE(REPLACE(address, '?', ''), '#', '') -- and many many other nested replaces
FROM table或者将这个逻辑放入应用程序中(这里是最好的选择)。
https://stackoverflow.com/questions/4990530
复制相似问题