Home  >  Article  >  Database  >  How to convert string to integer in mysql

How to convert string to integer in mysql

WBOY
WBOYOriginal
2022-05-12 17:44:337055browse

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

2. Use the CAST function

CAST(column as type);

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);

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

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!

Statement:
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
Previous article:What is mysql driverNext article:What is mysql driver