Get the App
SLTechnology News&Howtos  ›  Database  › 

How mysql converts varchar to int type

Shulou Source: shulou.com Published: 2022-05-31 10:44:10 10月16日 Update

This article mainly introduces "how mysql converts varchar to int type". In daily operation, I believe many people have doubts about how mysql converts varchar to int type. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how mysql converts varchar to int type"! Next, please follow the small series to learn together!

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

Operating environment of this tutorial: Windows 10 system, mysql version 8.0.22, Dell G3 computer.

How to convert varchar to int

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

1.CAST()

The MySQL CAST() function has the following syntax:

CAST(expression AS TYPE);

The CAST() function converts a value of any type 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 way 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 MySQL function CONVERT

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

At this point, the study of "how mysql converts varchar to int type" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Types functions learning methods syntax fields characters character sets more help different practical same next between two code manual tutorials data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker Shulou Tech Info Microsoft Apple