-- 创建一个函数
DELIMITER $$ DROP FUNCTION IF EXISTS `uExtractNumberFromString`$$ CREATE FUNCTION `uExtractNumberFromString`(in_string VARCHAR(50)) RETURNS VARCHAR(50) CHARSET utf8 NO SQL BEGIN DECLARE ctrNumber VARCHAR(50); DECLARE finNumber VARCHAR(50) DEFAULT ' '; DECLARE sChar VARCHAR(2); DECLARE inti INTEGER DEFAULT 1; IF LENGTH(in_string) > 0 THEN WHILE(inti <= LENGTH(in_string)) DO SET sChar= SUBSTRING(in_string,inti,1); SET ctrNumber= FIND_IN_SET(sChar,'0,1,2,3,4,5,6,7,8,9'); IF ctrNumber > 0 THEN SET finNumber=CONCAT(finNumber,''); ELSE SET finNumber=CONCAT(finNumber,sChar); END IF; SET inti=inti+1; END WHILE; RETURN finNumber; ELSE RETURN 0; END IF; END$$ DELIMITER ;
调用函数
SELECT uExtractNumberFromString(字段) FROM `表名`;