当前位置:网站首页>mysql的日期与时间函数,varchar与date相互转换
mysql的日期与时间函数,varchar与date相互转换
2022-07-28 05:08:00 【364.99°】
1.mysql获取系统时间
1.获取当前系统时间
在后面加上一个0,可以将当前查询到的时间转成数字输出。
1.获取当前日期+时间
函数:
| 函数 | 说明 |
|---|---|
now() | 函数开始执行就获取到值 |
sysdate() | 函数执行期间动态获取值 |
select now(), sysdate();

2.获取当前日期
| 函数 |
|---|
curdate() |
current_date() |
current_date |
select curdate(), current_date(), current_date, CURRENT_DATE() + 0;

3.获取当前时间
| 函数 |
|---|
curtime() |
current_time() |
current_time |
select curtime(), current_time(), current_time, current_time()+0;

4.获取UTC时间
select utc_date()+0, utc_time(), utc_timestamp;

2.获取昨天、今天、明天的,上一小时,下一小时的时间
1.昨天、今天、明天
日期相减
| 函数 | 说明 |
|---|---|
| date:合法的日期表达式 expr:时间间隔 type:second、hour、day、minute… |
日期 + 时间:
select date_sub(now(),interval 1 day) as '昨天',
date_sub(now(),interval 0 day) as '今天',
date_sub(now(),interval -1 day) as '明天'
日期:
select date_sub(curdate(),interval 1 day) as '昨天',
date_sub(curdate(),interval 0 day) as '今天',
date_sub(curdate(),interval -1 day) as '明天'

2.上一小时,前30分钟,后四十五分钟
select date_sub(now(),interval 1 hour) as '上一小时',
date_sub(now(),interval 30 minute) as '前30分钟',
date_sub(now(),interval 45 minute) as '后四十五分钟';

3.日期相减、时间相减
| 函数 | 说明 |
|---|---|
DATEDIFF(date1,date2) | date1:合法的日期表达式 date2:合法的日期表达式 |
TIMEDIFF(time1,time2) | time1:合法的时间表达式 time2:合法的时间表达式 |
select datediff(now(),date_sub(now(),interval 1 day)) as '日期相减';

select timediff(now(),date_sub(date_sub(now(),interval 45 minute),interval 1 day)) as '时间相减';

4.日期相加、时间相加
日期相加
| 函数 | 说明 |
|---|---|
DATE_ADD(date,INTERVAL expr type) | 见DATE_SUB |
TIMEDIFF(time1,time2) | time1:合法的时间表达式 time2:合法的时间表达式 |
select date_add(now(),interval 3 day) as '三天后',
date_add(now(),interval 30 minute) as '半小时后';

5.补充——sleep()
| 函数 | 说明 |
|---|---|
sleep() | 让SQL语句执行一段时间,单位是秒 |
select sleep(5), now(), sysdate();

2.mysql的varchar与date的转换
1.varchar转date
| 函数 | 格式 | 说明 |
|---|---|---|
| str_to_date | STR_TO_DATE(str,fmt) | str:字符串 fmt:时间格式 |
select str_to_date('2022 7 26','%Y %m %d') as '今天',
str_to_date('2022,27,7','%Y,%d,%m') as '明天',
str_to_date('2022725','%Y%m%d') as '昨天';

select str_to_date('26-7-2022-123456', '%d-%m-%Y') as '今天',
str_to_date('2022', '%Y') as '今年';

select str_to_date('221322','%H%i%s') as '现在1',
str_to_date('22:13:22','%H:%i:%s') as '现在2';

select str_to_date('221322','%H%i%s') as '现在1',
str_to_date('22:13:22','%H:%i:%s') as '现在2',
str_to_date('2022,7,26 22:13:22','%Y,%m,%e %H:%i:%s') as '现在3';

2.date转varchar
| 函数 | 格式 | 说明 |
|---|---|---|
| date_format | DATE_FORMAT(date,format) | date:合法的日期 format :规定日期/时间的输出格式 |
select date_format(now(), '%Y %m %d') as '今天',
date_format(now(), '%Y-%m-%d %H:%i:%s') as '现在';

3.补充
获取某天某一时刻的字符串
select date_format(trim(concat((select DATE_SUB(curdate(),INTERVAL 0 DAY)),' 00:00:00')), '%Y-%m-%d %H:%i:%s') as '今天凌晨',
date_format(trim(concat((select DATE_SUB(curdate(),INTERVAL 1 DAY)),' 00:00:00')), '%Y-%m-%d %H:%i:%s') as '昨天凌晨',
date_format(trim(concat((select DATE_SUB(curdate(),INTERVAL -1 DAY)),' 00:00:00')), '%Y-%m-%d %H:%i:%s') as '明天凌晨';

边栏推荐
- Can plastics comply with gb/t 2408 - Determination of flammability
- 【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
- Offline loading of wkwebview and problems encountered
- CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
- Visual studio 2019 new OpenGL project does not need to reconfigure the environment
- 【ARXIV2203】SepViT: Separable Vision Transformer
- FreeRTOS个人笔记-任务通知
- Look at the experience of n-year software testing summarized by people who came over the test
- 【计算机三级信息安全】信息安全保障概述
- Jsonp single sign on permission verification
猜你喜欢

What SaaS architecture design do you need to know?

Read the paper -- a CNN RNN framework for clip yield prediction

Testcafe provides automatic waiting mechanism and live operation mode

Improve the core quality of steam education among students

How to quickly locate bugs? How to write test cases?

Youxuan database participated in the compilation of the Research Report on database development (2022) of the China Academy of communications and communications

为什么md5不可逆,却还可能被md5免费解密网站解密

Visual studio 2019 new OpenGL project does not need to reconfigure the environment

Configuration experiment of building virtual private network based on MPLS

With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?
随机推荐
Angr (XI) - official document (Part2)
Leetcode 454. Adding four numbers II
HDU 3078 network (lca+ sort)
How to quickly turn function test to automatic test
Have you learned the common SQL interview questions on the short video platform?
Easycvr Video Square snapshot adding device channel offline reason display
Reading notes of SMT practical guide 1
Program life | how to switch to software testing? (software testing learning roadmap attached)
Clickhouse填坑记2:Join条件不支持大于、小于等非等式判断
App test process and test points
flink思维导图
go-zero单体服务使用泛型简化注册Handler路由
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
[internal mental skill] - creation and destruction of function stack frame (C implementation)
Reading sdwebimage source code Notes
Simulink automatically generates STM32 code details
Interpreting the source code of cfrunloopref
100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel
How to successfully test php7.1 connecting to sqlserver2008r2
Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam