Home > Database > Mysql Tutorial > body text

Mysql中实现提取字符串中的数字的自定义函数分享_MySQL

WBOY
Release: 2016-06-01 13:05:45
Original
1180 people have browsed it

因需要在mysql的数据表中某一字符串中的字段提取出数字,在网上找了一通,终于找到了一个可用的mysql函数,可以有效的从字符串中提取出数字。

该mysql提取出字符串中的数字函数如下:

代码如下:


CREATE FUNCTION GetNum (Varstring varchar(50))
RETURNS varchar(30)
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)) 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;


字段使用方法,如下:

代码如下:


select GetNum(字段)  from table


以一个字符串举例:

代码如下:


select GetNum("dfdfd666")


结果返回:666。

并且该函数可以提取出字符串中非连贯的数字,如:

代码如下:


select GetNum("dfdd111fd666")


结果返回:111666。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template