获取当前时间(sql执行开始的时间)
select now();
2021-04-07 22:23:01
获取当前时间(sql执行完成时的时间)
sysdate()
获取当前时间戳
current_timestamp
current_timestamp()
日期转字符串
select date_format('2021-04-07 22:23:01', '%Y%m%d%H%i%s');
20210407222301
字符串转日期
select str_to_date('2021/04/07 22:23:01', '%Y/%m/%d %H:%i:%s');
2021-04-07 22:23:01
获取从0000-00-00开始的天数(不包括时分秒)
select to_days('0000-00-00');
0
select to_days('2021-04-07 22:23:01');
738252
获取某一天的数据
select * from my_table where to_days(create_time)=to_days('2021-04-07');
从0000-00-00开始的天数转为日期
select from_days(738252);
2021-04-07
时间相减
select timediff('2021-04-07 08:08:08', '2021-04-07 00:00:00');
08:08:08
- 最大能计算时间差为838:59:59的时间,跨度再大也只返回该值。
- timediff(time1,time2) 函数的两个参数类型必须相同。
时间戳增减函数
select timestampadd(day, 1, '2021-04-07 08:08:08');
2021-04-08 08:08:08
select timestampdiff(year,'2001-01-01','2021-04-07');
20
select timestampdiff(day ,'2001-01-01','2021-04-07');
7401
select timestampdiff(hour,'2001-01-01 12:00:00','2021-04-07 12:00:00');
177624