当前位置:网站首页>[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
边栏推荐
- 是时候不得不学英语了,技多不压身,给自己多条路
- Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
- MySQL索引从入门到深入学习
- I went to meet some successful people worth tens of millions on May 1st, and I have some new ideas and inspirations
- 即刻报名|前沿技术探索:如何让 Spark 更强劲、更灵活
- Kyligence 再获 CRN, insideBIGDATA 两大国际奖项认可
- 《后浪》程序员版,献给新一代程序员的演讲,何冰《后浪》演讲模仿秀
- mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")
- JVM 类加载机制 超详细学习笔记(三)
- pytorch官网中如何选择以及后面的安装和pycharm测试步骤
猜你喜欢
随机推荐
期末作业C#实现学生宿舍管理系统
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
坠落的蚂蚁(北京大学考研机试题)
力扣1047-删除字符串中的所有相邻重复项——栈
go版本升级
SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
How can I make (a == 1 && a == 2 && a == 3) to be true?
Docker-compose install mysql
Nacos 原理
文档在线化管理系统Confluce使用
pycharm上的tensorflow环境搭载
postman 请求 post 调用 传 复合 json数据
个人博客系统(附源码)
上交所行情文件解析之mktdt04
暴力递归到动态规划 05 (贴纸拼词)
Path dependence: the poor hard science to counter attack breakthrough
golang八股文整理(持续搬运)
Mysql8.+学习笔记
从字节码角度带你彻底理解i++与++i
图形镜像对称(示意图)

![[其他] DS5](/img/20/6863bb7b58d2e60b35469ba32e5830.png)







![[Mysql] DATEDIFF函数](/img/cd/7d19e668701cdd5542b6e43f4c2ad4.png)