MySql常用函数之 convert()、str_to_date()、date_format()、to_base64()、from_base64()等使用介绍

一、convert()

1、将日期字符串转换为 date 类型

select convert('2024-01-06', date) from dual;

输出:

2024-01-06

2、将日期字符串转换为 datetime 类型

select convert('2024-01-06', datetime) from dual;

输出:

2024-01-06 00:00:00

3、将日期转换为 time 类型

select convert(now(), time) from dual;

输出:

23:35:51

4、将字符串转换为 decimal 类型

select convert('0.95', decimal(10, 2)) from dual;
select convert('99.05', decimal(10, 2)) from dual;

输出:

0.95

99.05

5、将数字转换为字符类型

select convert(1, char) from dual;

输出:

1

6、将字符串转换为数字类型

select convert('123', unsigned) from dual;
select ('123' + 0) as to_num from dual;

输出:

123

123

二、日期格式化 str_to_date()、date_format()

1、将日期字符串转换为date、datetime类型

select str_to_date('2024-01-06', '%Y-%m-%d') from dual;
select str_to_date('2024-01-06 13:15:25', '%Y-%m-%d %H:%i:%s') from dual;

输出:

2024-01-07

2024-01-07 13:15:25

2、将date、datetime转换为日期字符串

select date_format(now(), '%Y-%m-%d') from dual;
select date_format(now(), '%Y-%m-%d %H:%i:%s') from dual;

输出:

2024-01-07

2024-01-07 00:22:26

三、使用函数 to_base64()、from_base64() 对字符串编解码

1、to_base64() 编码

select to_base64('在干嘛了?') from dual;

输出:

5Zyo5bmy5Zib5LqGPw==

2、from_base64() 解码

select from_base64('5Zyo5bmy5Zib5LqGPw==') from dual;

输出:

在干嘛了?
相关推荐
cg50171 小时前
MySQL数据库复杂的增删改查操作
数据库·mysql
夏炎正好眠7 小时前
mysql练习
数据库·mysql
驜鸈9 小时前
MySQL 的EXPLAIN 计划 type 字段详细说明
android·数据库·mysql
嗨起飞了10 小时前
MySQL入门手册
数据库·mysql
程序员的世界你不懂11 小时前
Mysql配置文件My.cnf(my.ini)配置参数说明
数据库·mysql·百度·新浪微博
ChinaRainbowSea11 小时前
MySQL 索引的数据结构(详细说明)
java·数据结构·数据库·后端·mysql
追风赶月、11 小时前
【MySQL】事务(隔离性、MVCC)
数据库·mysql
Lemon_man_12 小时前
基于Django创建一个WEB后端框架(DjangoRestFramework+MySQL)流程
python·mysql·django
A仔不会笑14 小时前
MySQL面试篇——性能优化
java·数据库·mysql·面试·性能优化
考虑考虑15 小时前
MySQL中的DATE_FORMAT时间函数
数据库·后端·mysql