Home > Database > Mysql Tutorial > How to convert mysql string to number

How to convert mysql string to number

藏色散人
Release: 2021-12-14 17:40:09
Original
26766 people have browsed it

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.

How to convert mysql string to 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.

How to convert mysql string to number

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);
Copy after login

You can see that the result directly converts the string '5.45' into the number 5 because SIGNED represents an integer.

How to convert mysql string to number

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.

How to convert mysql string to number

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);
Copy after login

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

How to convert mysql string to number

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));
Copy after login

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

How to convert mysql string to number

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);
Copy after login

Running results As shown below.

How to convert mysql string to number

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.

How to convert mysql string to number

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template