当前位置:网站首页>[Mysql] CONVERT function
[Mysql] CONVERT function
2022-07-30 05:55:00 【iijik55】
CONVERT function is used to convert a value to the specified data type or character set
1. Convert the specified character set
The CONVERT function is used to convert the character set of the string expr into transcoding_name
Syntax structure
CONVERT(expr USING transcoding_name)
expr: value to convert
transcoding_name: character set to convert to
-- utf8mb4SELECT CHARSET('ABC');--gbkSELECT CHARSET(CONVERT('ABC' USING gbk));2. Convert the specified data type
The CONVERT function is used to convert the character set of the string expr into transcoding_name
Syntax structure
CONVERT(expr,type)
expr: value to convert
type: The data type to convert to
value of the type parameter
value
Description
DATE
Convert expr to 'YYYY-MM-DD' format
DATETIME
Convert expr to 'YYYY-MM-DD HH:MM:SS' format
TIME
Convert expr to 'HH:MM:SS' format
CHAR
Convert expr to CHAR (fixed-length string) format
SIGNED
Convert expr to INT (signed integer) format
UNSIGNED
Convert expr to INT (unsigned integer) format
DECIMAL
Convert expr to FLOAT (floating point) format
BINARY
Convert expr to binary format
Example
1. Convert the value to DATE data type
-- 2022-05-25SELECT CONVERT('2022-05-25', DATE);-- 2022-05-25 17:58:48SELECT NOW();-- 2022-05-25SELECT CONVERT(NOW(), DATE);2. Convert the value to DATETIME data type
-- 2022-05-25 00:00:00SELECT CONVERT('2022-05-25', DATETIME);3. Convert the value to the TIME data type
-- 14:06:10SELECT CONVERT('14:06:10', TIME);-- 2022-05-25 17:25:12SELECT NOW();-- 17:25:12SELECT CONVERT(NOW(), TIME);4. Convert the value to CHAR data type
-- '150'SELECT CONVERT(150, CHAR);-- errorSELECT CONCAT('Hello World',437));-- 'Hello World437'SELECT CONCAT('Hello World',CONVERT(437, CHAR));5. Convert the value to the SIGNED data type
-- 5SELECT CONVERT('5.0', SIGNED);-- 2SELECT (1 + CONVERT('3', SIGNED))/2;-- -5SELECT CONVERT(5-10, SIGNED);-- 6SELECT CONVERT(6.4, SIGNED);-- -6SELECT CONVERT(-6.4, SIGNED);-- 7SELECT CONVERT(6.5, SIGNED);-- -7SELECT CONVERT(-6.5, SIGNED);6. Convert the value to UNSIGNED data type
-- 5SELECT CONVERT('5.0', UNSIGNED);-- 6SELECT CONVERT(6.4, UNSIGNED);-- 0SELECT CONVERT(-6.4, UNSIGNED);-- 7SELECT CONVERT(6.5, UNSIGNED);-- 0SELECT CONVERT(-6.5, UNSIGNED);7. Convert the value to DECIMAL data type
-- 9SELECT CONVERT('9.0', DECIMAL);-- DECIMAL (numeric precision, decimal point retention length)-- DECIMAL(10,2) can store numbers with up to 8 integers and 2 decimals-- Precision and scale are 10 and 2, respectively-- Precision is the total number of digits, including the sum of digits to the left and right of the decimal point-- The number of decimal places is the number of digits to the right of the decimal point-- 9.50SELECT CONVERT('9.5', DECIMAL(10,2));-- 99999999.99SELECT CONVERT('1234567890.123', DECIMAL(10,2));-- 220.232SELECT CONVERT('220.23211231', DECIMAL(10,3));-- 220.232SELECT CONVERT(220.23211231, DECIMAL(10,3));CAST function can also realize data type conversion
Supplementary Information | CAST Function
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
- Docker-compose安装mysql
- 从驱动表和被驱动表来快速理解MySQL中的内连接和外连接
- MySQL(4)
- RadonDB MySQL on K8s 2.1.3 发布!
- 容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
- 开源之夏 2022 重磅来袭!欢迎报名 RadonDB 社区项目!
- An old programmer's summary review of 2020, how to become more awesome in 2021
- [其他] DS5
猜你喜欢
随机推荐
开源之夏 2022 重磅来袭!欢迎报名 RadonDB 社区项目!
微信小程序开发学习
个人博客系统(附源码)
【Koltin Flow(一)】五种创建flow的方式
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
Path dependence: the poor hard science to counter attack breakthrough
程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱
1475. 商品折扣后的最终价格
2022鹏城杯web
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
leetcode hot 100(刷题篇11)(231/235/237/238/292/557/240/36)offer/3/4/5
C language implements highly secure game archives and reads files
力扣05-替换空格——字符串问题
Participate in open source, let programmers regain their blood and passion
JVM之GC 调优基础知识(一)
Programmers care guide, give yourself a chance to make the occasional relaxation of body and mind
一个老程序员的2020年总结回顾,2021年如何变的更牛逼
容器化|在 S3 备份恢复 RadonDB MySQL 集群数据
4、nerf(pytorch)
[Redis Master Cultivation Road] Jedis - the basic use of Jedis









