Home  >  Article  >  Database  >  How to convert varchar to int type in mysql

How to convert varchar to int type in mysql

WBOY
WBOYOriginal
2022-05-12 16:51:1714697browse

Conversion method: 1. Use the cast function, the syntax is "select * from table name order by cast (field name as SIGNED)"; 2. Use "select * from table name order by CONVERT (field name, SIGNED)" )" statement.

How to convert varchar to int type in mysql

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

How does mysql convert varchar to int type

There are two functions in mysql that can perform type conversion:

1.CAST()

The syntax of the MySQL CAST() function is as follows:

CAST(expression 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.CONVERT()

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

The syntax is:

CONVERT(expr USING transcoding_name)

In MySQL, the conversion code name is the same as the corresponding character set name.

Examples are as follows:

1. Manual conversion type (direct 0)

select server_id from cardserver where game_id = 1 order by server_id+0 desclimit 10

2. Use MySQL function CAST

select server_id from cardserver where game_id = 1 order by CAST(server_id as SIGNED) desc limit 10;

3. Use the MySQL function CONVERT

select server_id from cardserver where game_id = 1 order by CONVERT(server_id,SIGNED)desc limit 10;

How to convert varchar to int type in mysql

How to convert varchar to int type in mysql

Recommended learning: mysql video tutorial

The above is the detailed content of How to convert varchar to int type 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-connectorNext article:What is mysql-connector