当前位置:网站首页>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 '明天凌晨';

边栏推荐
- 【ARXIV2205】Inception Transformer
- Autoreleasepool problem summary
- Inspire domestic students to learn robot programming education for children
- After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
- After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!
- 数据库日期类型全部为0
- 【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution
- php7.1 连接sqlserver2008r2 如何测试成功
- Read the paper -- a CNN RNN framework for clip yield prediction
- MySQL(5)
猜你喜欢

The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?

阿里怎么用DDD来拆分微服务?

Simulink automatically generates STM32 code details

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

Automated test tool playwright (quick start)

【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation

Inspire domestic students to learn robot programming education for children

Win10 machine learning environment construction pycharm, anaconda, pytorch

flink思维导图

100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel
随机推荐
HashSet add
What tools do software testers need to know?
Know etcd
From the basic concept of micro services to core components - explain and analyze through an example
Can plastics comply with gb/t 2408 - Determination of flammability
Pipe /createpipe
Database date types are all 0
I interviewed a 38 year old programmer and refused to work overtime
Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam
Applet import project
Data security is gradually implemented, and we must pay close attention to the source of leakage
Why is MD5 irreversible, but it may also be decrypted by MD5 free decryption website
[internal mental skill] - creation and destruction of function stack frame (C implementation)
【ARXIV2205】Inception Transformer
HDU 1522 marriage is stable
Interpretation of afnetworking4.0 request principle
HDU 2874 connections between cities
UI automation test farewell from now on, manual download browser driver, recommended collection
【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers
Table image extraction based on traditional intersection method and Tesseract OCR