当前位置:网站首页>Data type conversion in MySQL 8 | learning functions cast() and convert()
Data type conversion in MySQL 8 | learning functions cast() and convert()
2022-07-22 12:14:00 【Navicat China】
Whenever data is classified into different types , It is inevitable to convert from one data type to another . In my mind , A common use example is processing through query parameters or POST Request body from Web Variables passed in from the form . Serializing data for sending over the network tends to cast all variables into strings . therefore , They usually need to be converted to a more appropriate data type , For example, digital 、 Date or data type you have .
In a relational database , Reasons for converting one data type to another include migrating data from one database type to another 、 Change the data type of the column or temporarily switch between data types for calculation . stay MySQL in , We can use CAST() and CONVERT() Function to convert between data types . In today's article , We will learn how to use these two functions , And illustrate their usage through examples .
What's the difference ?
CAST() and CONVERT() Can be changed MySQL Data types in . Because the two are very similar , many SQL Novice ( And some more experienced users !) Want to know the difference . The main difference is that CONVERT() You can also convert the character set of data into another character set .CAST() Cannot be used to change the character set . therefore , Unless you need to convert the character set ,CAST() It should be yours goto Conversion function .
CAST() function
MySQL CAST() Accept two inputs :
- Data to be typed
- The data type you want to convert this data to ( Decimal system 、 Character etc. ). You can convert data into BINARY、CHAR、DATE、DATETIME、TIME、DECIMAL、SIGNED、UNSIGNED data type .
H This is grammar :
CAST(data as data_type)
Real examples
CAST() A useful application of functions is to make very large data types less cumbersome ( More practical ?). The following query returns information about MySQL Sakila Information about specific movies in the sample database . One of its columns description Is a text field . This means that it can store a large amount of text ! We can use CAST() Truncate the description to 100 Characters , So we won't get the whole book about movies :
Speaking of Sakila Sample database , You know it takes MySQL Is the dolphin mascot named ? It is from the user in “Name the Dolphin” Selected from a large number of names suggested in the competition . The name of the award is from Africa Eswatini( front Swaziland) Open source software developers Ambrose Twebaze Submit .
CONVERT() function
CONVERT() The syntax of function and CAST() similar , But the format of expression and result type is slightly different . One way is to provide two separate parameters :
CONVERT(expr, data_type)
besides ,data_type Parameters can be CAST() Any of the same types supported by the function .
Real examples
because CAST() and CONVERT() The main difference between them is that the latter can convert the character set of columns into different character sets , Let's actually demonstrate .
The first thing to notice is , The syntax of converting character sets is slightly different . under these circumstances , We need to add USING keyword :
CONVERT(expr USING charset);
stay Navicat for MySQL( or Navicat Premium) in , We can see the character set and sorting rules of the table in the information pane :
Consider this , We can CONVERT() Function is applied to the previous query , take description Fields from UTF-8 Convert to Latin1. If you're curious , The difference between the two is , stay latin1 in , Each character is exactly one byte long , And in the utf8 in , A character can consist of multiple bytes . therefore utf8 Than latin1 There are more characters . Besides , The characters they share are not necessarily represented by the same bytes or byte sequences .
summary
In today's article , We saw how to use CAST() Convert data to different types , And how to use it CONVERT() Convert between character sets . To reiterate ,CAST() It should be yours goto Conversion function . and CONVERT() It is more suitable for switching between character sets . If you are right about Navicat for MySQL Interested in , Free trial 14 God !
Looking back
Navicat Poisoned | The truth is coming !
Equipment paralysis caused by piracy
Navicat 16.1 by OceanBase Community Edition
Navicat Become a member of the database Innovation Laboratory of the Academy of communications
Navicat Academic partnership program - Free education application
Navicat Technology think tank - Practical exercises and answers to various hot questions
Free trial introduction | Navciat 16 Database management tools
边栏推荐
- 经典网络架构学习-ResNet
- Flutter实战-自定义键盘(三)
- 【机器学习的数学01】可数集与不可数集
- Sentinel vs Hystrix 限流对比,该如何选择?
- 【oops-framework】日志管理
- User identity authentication of industrial Internet based on HHT transformation of mouse behavior
- How to choose the primary key UUID, self incrementing ID and snowflake algorithm of MySQL? (glory Collection Edition)
- 6.paddlepaddle之自定义模型构建使用
- Pyqt5 makes an exquisite video player * with source code
- [exercise C] how many steps does it take to find a number at least? It can be changed into Fibonacci number
猜你喜欢
6. User defined model construction and use of paddlepaddle
[development tutorial 5] open source Bluetooth heart rate waterproof sports Bracelet - battery power detection
Do you dare to use BigDecimal without mastering these pits?
How does redis like and cancel? (glory Collection Edition)
SCA在得物DevSecOps平台上应用
Flutter实战-自定义键盘(二)
Pyqt5 makes an exquisite video player * with source code
Mapping of cyberspace assets
31. [memcpy function and strcpy function]
传奇架设教程GOM引擎微端设置方法
随机推荐
Research on Lora network security scheme based on RF fingerprint
For() loop and quadratic for nesting of loop structure
LSA类型,OSPF的优化以及拓扑配置
Figure neural network driven traffic prediction technology: Exploration and challenge
Bloom filter and cuckoo filter (classic version)
不掌握这些坑,你敢用BigDecimal吗?
第01篇:分布式注册中心
FPGA——SPI总线控制flash(1)(含代码)
数据库索引的缺点
MySQL 8 中的数据类型转换 | 学习函数CAST() 和 CONVERT()
第02篇:分布式负载均衡
[oops framework] global event
Uniapp encapsulation request
Video 46 13.9 Semantic segmentation and data set 13.10 transpose convolution
JVM参数配置说明
NFS shared storage service
Application of SCA on devsecops platform
From basic to advanced, 100 test and development interview questions are necessary for entering a large factory to raise salary
Opencv将一个文件夹下所有图片合成视频
传奇架设教程GOM引擎微端设置方法