Mysql string to number method: 1. Use the "CAST(value AS type);" method to convert the string into a number; 2. Use "SELECT CAST('5.45' AS DECIMAL(9,2 ));" Convert a string to a floating point number.
The operating environment of this article: Windows 7 system, Mysql version 5.7.14, Dell G3 computer.
How to convert mysql string to number?
Mysql string to number:
The simplest way is directly The 0 after the string is equivalent to directly converting the string into a numeric type. Let's take a look at the specific operations. You can see that through the 0 operation, the two strings are successfully converted into numbers, and the result after addition is obtained. the result of.
Use the CAST() function, the usage method is CAST(value AS type);, you can take a look at the specific operation examples below, and check the results through the following sql statement:
SELECT CAST('5.45' AS SIGNED);
You can see that the result directly converts the string '5.45' into the number 5 because SIGNED represents an integer.
If you want to convert the string '5.45' into a floating point number 5.45, you can use DECIMAL, then the sql statement will be changed to the following:
SELECT CAST ('5.45' AS DECIMAL(9,2)); , 9 and 2 represent precision and number of decimal places respectively, as shown in the figure.
Use the CONVERT(value, type); method. You can see the specific operations below and view the results through the following sql statement:
SELECT CONVERT('67',SIGNED);
You can see The result is that the string '67' is directly converted into the number 67, as shown in the figure.
Similarly, if you want to use the CONVERT method to convert a string with decimals into a floating point number, you can use DECIMAL directly. Take a look at the example sql below:
SELECT CONVERT('67.35',DECIMAL(9,2));
The result can also be successfully converted, and the running result is as shown in the figure.
If you want to perform the opposite operation, that is, convert the number into a string, you can use CHAR and look at the sql statement:
SELECT CONVERT(23,CHAR);
Running results As shown below.
To convert numbers into strings, you can also directly use the "''" method. The sql statement is as follows:
SELECT 123 '';, the The running results of sql are as shown in the figure.
[Related recommendations: mysql video tutorial]
The above is the detailed content of How to convert mysql string to number. For more information, please follow other related articles on the PHP Chinese website!