当前位置:网站首页>[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
边栏推荐
猜你喜欢
随机推荐
MySQL索引从入门到深入学习
SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
Within the SQL connection table (link connections, left or right, cross connection, full outer join)
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
Seata exception: endpoint format should like ip:port
Redis学习
为Bitbucket 和 Sourcetree 设置SSL认证
【LeetCode】Day107-除自身以外数组的乘积
图形镜像对称(示意图)
Nacos 原理
分布式事务之 Seata框架的原理和实战使用(三)
微信支付及支付回调
容器化 | 在 KubeSphere 中部署 MySQL 集群
Programmers care guide, give yourself a chance to make the occasional relaxation of body and mind
pycharm上的tensorflow环境搭载
Programmers make money and practice, teach you how to do paid courses, self-media, paid articles and paid technical courses to make money
解决没有配置本地nacos但是一直发生localhost8848连接异常的问题
开源之夏 2022 重磅来袭!欢迎报名 RadonDB 社区项目!
pytorch官网中如何选择以及后面的安装和pycharm测试步骤
MySQL基础(DDL、DML、DQL)









