MYSQL FUNCTION : regex replace
CREATE FUNCTION `regex_replace`(`pattern` VARCHAR(1000), `replacement` VARCHAR(1000), `original` VARCHAR(1000) CHARSET utf8) RETURNS varchar(1000) CHARSET utf8
DETERMINISTICBEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(1);
DECLARE i INT;SET i = 1;SET temp = '';IF original REGEXP pattern THEN loop_label:
LOOP
IF i>CHAR_LENGTH(original) THENLEAVE loop_label;
END IF;SET ch = SUBSTRING(original,i,1);IF NOT ch REGEXP pattern THENSET temp = CONCAT(temp,ch);ELSESET temp = CONCAT(temp,replacement);END IF;SET i=i+1;END LOOP;ELSESET temp = original;END IF;RETURN temp;END;
usege :
/* Remove all non numeric characters */
UPDATE table SET `column`=regex_replace('[^0-9]','',column);
No comments:
Post a Comment