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 data types

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

Share

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

This article mainly introduces "how to convert data types in mysql". In daily operation, I believe many people have doubts about how to convert data types in mysql. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to convert data types in mysql". Next, please follow the editor to study!

Mysql data type conversion methods: 1, the use of cast () function to convert data types, syntax for "cast (field name as conversion type)"; 2, the use of convert () function to convert data types, syntax for "convert (field name, converted type)".

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

How mysql converts data types

The CAST () and CONVERT () functions of MySQL can be used to get a value of one type and produce a value of another type. That is, to convert data types.

The specific syntax of the two is as follows:

CAST (value as type); CONVERT (value, type)

It is CAST (xxx AS type), CONVERT (xxx, type).

There are limits to the types that can be converted. This type can be one of the following values:

Binary, with binary prefix effect: BINARY character type, with parameters: CHAR () date: DATE time: TIME date time type: DATETIME floating point number: DECIMAL integer: SIGNED unsigned integer: UNSIGNED

Here are a few examples:

Mysql > SELECT CONVERT; +-- + | CONVERT ('23-signed) | +-- + | 23 | +-- + 1 row in set

Example two

Mysql > SELECT CAST ('125e342.83' AS signed) +-- + | CAST ('125e342.83' AS signed) | +-+ | 125 | +-+ 1 row in set

As in the example above, convert varchar to int with cast (an as signed), where an is a string of type varchar.

In SQL Server, the following code demonstrates that the hexadecimal storage of the date store represents the result when the datetime variable contains only a simple date and a simple time.

DECLARE @ dt datetime-simple date SET @ dt='1900-1-2'SELECT CAST (@ dt as binary (8))-result: 0x0000000100000000-simple time SET @ dt='00:00:01'SELECT CAST (@ dt as binary (8))-result: 0x000000000000012C

The type conversion of MySQL is the same as SQL Server, except that the type parameters are slightly different: CAST (xxx AS type), CONTVER (xxx, type).

At this point, the study on "how to convert data types in mysql" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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