mysql字段中提取数字
CREATE FUNCTION GetNum (Varstring varchar(50)) RETURNS varchar(30) DETERMINISTIC BEGIN DECLARE v_length INT DEFAULT 0; DECLARE v_Tmp varchar(50) default ''; set v_length=CHAR_LENGTH(Varstring); WHILE v_length > 0 DO IF (ASCII(mid(Varstring,v_length,1))>47 and ASCII(mid(Varstring,v_length,1))<58 ) THEN set v_Tmp=concat(v_Tmp,mid(Varstring,v_length,1)); END IF; SET v_length = v_length - 1; END WHILE; RETURN REVERSE(v_Tmp); END;
有些版本下面语句不好使 请用上面语句
DELIMITER $$ USE `eps` $$ DROP FUNCTION IF EXISTS `fn_GetNum` $$ CREATE FUNCTION `fn_GetNum` (Varstring VARCHAR (500)) RETURNS VARCHAR (30) CHARSET utf8mb4 BEGIN DECLARE v_length INT DEFAULT 0 ; DECLARE v_Tmp VARCHAR (50) DEFAULT '' ; SET v_length = CHAR_LENGTH(Varstring) ; WHILE v_length > 0 DO IF ( ASCII(MID(Varstring, v_length, 1)) > 47 AND ASCII(MID(Varstring, v_length, 1)) < 58 ) THEN SET v_Tmp = CONCAT(v_Tmp, MID(Varstring, v_length, 1)) ; END IF ; SET v_length = v_length - 1 ; END WHILE ; RETURN REVERSE(v_Tmp) ; END $$