Share custom functions that extract numbers from strings in Mysql, mysql strings

Source: Internet
Author: User

Share custom functions that extract numbers from strings in Mysql, mysql strings

Because a number needs to be extracted from a field in a string in the mysql DATA table, I found it online and finally found an available mysql function, which can effectively extract numbers from the string.

The numeric function in the mysql extract string is as follows:
Copy codeThe Code is as follows:
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) <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;

Field Usage:
Copy codeThe Code is as follows:
Select GetNum (field) from table

Take a string as an example:
Copy codeThe Code is as follows:
Select GetNum ("dfdfd666 ")

Result: 666.

In addition, this function can extract non-coherent numbers in a string, such:
Copy codeThe Code is as follows:
Select GetNum ("dfdd111fd666 ")

Result: 111666.


How does MYSQL extract numbers from strings? That is, to filter out non-numeric characters

Mysql cannot be used for single storage. If php is added, it can be implemented!
Php can directly add data + 0 to retrieve the numbers in the string ~

How to extract numbers from strings

Yes. Use the find function to locate and find the character location, and then use the LEFT function to extract it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.