Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How mysql converts varchar to int type

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report