Get the App
SLTechnology News&Howtos  ›  Database  › 

How to convert mysql strings to numbers

Shulou Source: shulou.com Published: 2022-05-31 12:48:46 10月18日 Update

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.

Tags: Characters strings numbers results statements methods such as figure method point article run success representative decimal more good practical two that is digits Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia Shulou Technology Docker vpn