Home > Database > Mysql Tutorial > body text

How to convert string to integer in mysql

WBOY
Release: 2022-05-12 17:53:00
Original
7057 people have browsed it

Method: 1. Use "String 0" to convert, the syntax is "(column 0)"; 2. Use the CONVERT function to convert, the syntax is "CONVERT(column,SIGNED)"; 3. Use the CAST function Conversion, the syntax is "CAST(column as SIGNED)".

How to convert string to integer in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How to convert a string to an integer in mysql

1. Directly use addition

String 0

The example is as follows

example:
select * from orders order by (column+0) desc
Copy after login

2. Use the CAST function

CAST(column as type);
Copy after login

The CAST() function converts any type of value to a value of the specified type. The target type can be one of the following types: BINARY, CHAR, DATE, DATETIME, TIME, DECIMAL, SIGNED, UNSIGNED.

2. Use the CONVERT function

CONVERT(column, type);
Copy after login

MySQL CONVERT() provides a method to convert data between different character sets.

Note:

The type here can be:

Floating point number: DECIMAL

 Integer: SIGNED

  Unsigned integer: UNSIGNED

example:
select * from orders order by CONVERT(column,SIGNED) desc
select * from orders order by CAST(column as SIGNED) desc
Copy after login

Recommended learning: mysql video tutorial

The above is the detailed content of How to convert string to integer in mysql. 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