Get the App
SLTechnology News&Howtos  ›  Database  › 

How does MySQL extract numeric values from strings

Shulou Source: shulou.com Published: 2022-05-31 18:47:14 10月02日 Update

This article introduces the knowledge of "how to extract values from a string by MySQL". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

MySQL has so many string functions that sometimes I don't know how to use them flexibly.

String basic information function collation convert,char_length, etc.

Encryption functions password (x), encode, aes_encrypt

String concatenation function concat (x1thine x2, … (.)

Pruning function trim,ltrim,rtrim

Substring operation functions substring (XMagne start length), mid (x Magne start length)

String copy function repeat,space

String comparison function strcmp

String reverse function reverse

If you really give a scene, you might be able to take a picture of the chest.

Suppose I have the following requirements, such as registering an account in an email. The specified account starts with a number, and the content is as follows:

1234@mail.com

012345@aa.mail.com

1234mm@mail.com

1234test@mail.com

If you need to extract the numbers inside, is there any good way?

If you use string functions, one way is to use regular, or directly given conditions for filtering.

For example, replace (xxxx,right (xxx))

Another idea is to create a function or stored procedure and do the transformation in a structured way.

As a matter of fact, all of the above methods are troublesome. What else can I do? I will give two examples.

The first solution is to use the data type conversion of strings.

For example:

Mysql > select cast ('123456roomxx.com' as unsigned)

+-+

| | cast ('123456roomxx.com' as unsigned) |

+-+

| | 123456 |

+-+

1 row in set, 1 warning (0.00 sec)

We can clearly see the results and a warning.

Mysql > show warnings

+-- +

| | Level | Code | Message | |

+-- +

| | Warning | 1292 | Truncated incorrect INTEGER value: '123456 / 163.com' | |

+-- +

1 row in set (0.00 sec)

Solution 2:

This solution is simpler and has a feeling of miraculous craftsmanship.

Mysql > select-(- '123456 / 163.com')

+-+

| |-(- '123456 / 163.com') |

+-+

| | 123456 |

+-+

1 row in set, 1 warning (0.00 sec)

If there are redundant numbers in front of them, they can also be converted.

Mysql > select-(- 012345roomaa.mail.com')

+-+

| |-(- '012345roomaa.mail.com') |

+-+

| | 12345 |

+-+

1 row in set, 1 warning (0.00 sec)

That's all for "how MySQL extracts values from strings". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Function character string content that is number solution numerical value extraction method method more knowledge account process obviously there is a kind of example miraculous craftsmanship successful learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS MariaDB Docker NVidia