Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert mysql strings to numbers

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to convert mysql strings to numbers. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

mysql string to number method: 1, use "CAST(value AS type);" method to convert the string to a number;2, use "SELECT CAST ('5.45 ' AS DECIMAL(9,2));" to convert the string to a floating point number.

Operating environment: Windows 7 system, Mysql 5.7.14 version, Dell G3 computer.

mysql string to number:

The simplest way is to directly after the string +0, which is equivalent to directly converting the string to a numeric type. Below you can see the specific operation. You can see that through the +0 operation, you successfully convert the two strings to numbers and get the result after adding them.

Use CAST() function, using CAST(value AS type);, the following can look at the specific operation example, through the following sql statement to view the results:

SELECT CAST('5.45' AS SIGNED);

You can see that the result directly converts the string '5.45' to the number 5 because SIGNED stands for an integer.

If you want to convert the string '5.45' to the floating-point number 5.45, you can use DECIMAL, so the sql statement is changed as follows:

SELECT CAST ('5.45 ' AS DECIMAL(9,2));, 9 and 2 represent precision and decimal places, respectively, as shown.

Using CONVERT(value, type); method, you can see the specific operation below, through the following sql statement to see the results:

SELECT CONVERT('67',SIGNED);

You can see that the result directly converts the string '67' into the number 67, as shown in the figure.

Similarly, if you want to convert a string with decimal numbers to floating point numbers using CONVERT, you can use DECIMAL directly. Here is an example sql:

SELECT CONVERT('67.35',DECIMAL(9,2));

The result can also be successfully converted, and the running result is shown in the figure.

If you want to do the opposite, that is, convert numbers to strings, you can use CHAR. Look at the sql statement:

SELECT CONVERT(23,CHAR);

The results are shown below.

To convert numbers into strings, you can also use the "+""method directly. The sql statement is as follows:

SELECT 123+'';, the result of running this sql is shown in the figure.

About "mysql string how to turn numbers" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report