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

边栏推荐
- 【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution
- Gym 101911c bacteria (minimum stack)
- 100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel
- Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
- CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
- The go zero singleton service uses generics to simplify the registration of handler routes
- The research group passed the thesis defense successfully
- HDU 3078 network (lca+ sort)
- HDU 2586 How far away ? (LCA multiplication method)
- SMD component size metric English system corresponding description
猜你喜欢

Offline loading of wkwebview and problems encountered

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

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!

【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers

From the basic concept of micro services to core components - explain and analyze through an example

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

你必需要了解的saas架构设计?

【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention

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

FreeRTOS learning (I)
随机推荐
Dynamic SQL and paging
【CPU占用高】software_reporter_tool.exe
How does Alibaba use DDD to split microservices?
Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
Introduction to testcafe
【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
The go zero singleton service uses generics to simplify the registration of handler routes
Keil Chinese garbled code solution
Program life | how to switch to software testing? (software testing learning roadmap attached)
从微服务基本概念到核心组件-通过一个实例来讲解和分析
Automated test tool playwright (quick start)
What is the core value of testing?
Reading notes of SMT practical guide 1
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?
【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
如何在 FastReport VCL 中通过 Outlook 发送和接收报告?
Redux basic syntax
HDU 3078 network (lca+ sort)
Array or object, date operation
Testcafe provides automatic waiting mechanism and live operation mode