当前位置:网站首页>MySQL date and time function, varchar and date are mutually converted
MySQL date and time function, varchar and date are mutually converted
2022-07-28 05:11:00 【364.99°】
1.mysql Get system time
1. Get current system time
Add one after that 0, You can convert the current queried time into digital output .
1. Get current date + Time
function :
| function | explain |
|---|---|
now() | The function gets the value at the beginning of execution |
sysdate() | Get values dynamically during function execution |
select now(), sysdate();

2. Get current date
| function |
|---|
curdate() |
current_date() |
current_date |
select curdate(), current_date(), current_date, CURRENT_DATE() + 0;

3. Get the current time
| function |
|---|
curtime() |
current_time() |
current_time |
select curtime(), current_time(), current_time, current_time()+0;

4. obtain UTC Time
select utc_date()+0, utc_time(), utc_timestamp;

2. Get yesterday 、 today 、 Tomorrow's , Last hour , The next hour
1. yesterday 、 today 、 Tomorrow,
Date subtraction
| function | explain |
|---|---|
| date: Legal date expression expr: The time interval type:second、hour、day、minute… |
date + Time :
select date_sub(now(),interval 1 day) as ' yesterday ',
date_sub(now(),interval 0 day) as ' today ',
date_sub(now(),interval -1 day) as ' Tomorrow, '
date :
select date_sub(curdate(),interval 1 day) as ' yesterday ',
date_sub(curdate(),interval 0 day) as ' today ',
date_sub(curdate(),interval -1 day) as ' Tomorrow, '

2. Last hour , front 30 minute , 45 minutes later
select date_sub(now(),interval 1 hour) as ' Last hour ',
date_sub(now(),interval 30 minute) as ' front 30 minute ',
date_sub(now(),interval 45 minute) as ' 45 minutes later ';

3. Date subtraction 、 Time minus
| function | explain |
|---|---|
DATEDIFF(date1,date2) | date1: Legal date expression date2: Legal date expression |
TIMEDIFF(time1,time2) | time1: Legal time expression time2: Legal time expression |
select datediff(now(),date_sub(now(),interval 1 day)) as ' Date subtraction ';

select timediff(now(),date_sub(date_sub(now(),interval 45 minute),interval 1 day)) as ' Time minus ';

4. Date addition 、 Time adds up
Date addition
| function | explain |
|---|---|
DATE_ADD(date,INTERVAL expr type) | see DATE_SUB |
TIMEDIFF(time1,time2) | time1: Legal time expression time2: Legal time expression |
select date_add(now(),interval 3 day) as ' Three days later ',
date_add(now(),interval 30 minute) as ' After half an hour ';

5. Add ——sleep()
| function | explain |
|---|---|
sleep() | Give Way SQL The statement executes for a period of time , The unit is seconds |
select sleep(5), now(), sysdate();

2.mysql Of varchar And date Transformation
1.varchar turn date
| function | Format | explain |
|---|---|---|
| str_to_date | STR_TO_DATE(str,fmt) | str: character string fmt: Time format |
select str_to_date('2022 7 26','%Y %m %d') as ' today ',
str_to_date('2022,27,7','%Y,%d,%m') as ' Tomorrow, ',
str_to_date('2022725','%Y%m%d') as ' yesterday ';

select str_to_date('26-7-2022-123456', '%d-%m-%Y') as ' today ',
str_to_date('2022', '%Y') as ' This year, ';

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

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

2.date turn varchar
| function | Format | explain |
|---|---|---|
| date_format | DATE_FORMAT(date,format) | date: The legal date format : Specified date / Time output format |
select date_format(now(), '%Y %m %d') as ' today ',
date_format(now(), '%Y-%m-%d %H:%i:%s') as ' Now? ';

3. Add
Get the string of a certain day and a certain time
select date_format(trim(concat((select DATE_SUB(curdate(),INTERVAL 0 DAY)),' 00:00:00')), '%Y-%m-%d %H:%i:%s') as ' Early this morning ',
date_format(trim(concat((select DATE_SUB(curdate(),INTERVAL 1 DAY)),' 00:00:00')), '%Y-%m-%d %H:%i:%s') as ' In the morning yesterday ',
date_format(trim(concat((select DATE_SUB(curdate(),INTERVAL -1 DAY)),' 00:00:00')), '%Y-%m-%d %H:%i:%s') as ' Tomorrow morning ';

边栏推荐
- [learning record] data enhancement 1
- I interviewed a 38 year old programmer and refused to work overtime
- C language classic 100 question exercise (1~21)
- 面试了一位38岁程序员,听说要加班就拒绝了
- jsonp 单点登录 权限检验
- Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
- Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or
- Improve the core quality of steam education among students
- Online sql to XML tool
- Offline loading of wkwebview and problems encountered
猜你喜欢

Easycvr Video Square snapshot adding device channel offline reason display

数据库日期类型全部为0

【CVPR2022】On the Integration of Self-Attention and Convolution

Inspire domestic students to learn robot programming education for children

flink思维导图

What SaaS architecture design do you need to know?

Service object creation and use

Know etcd
![[internal mental skill] - creation and destruction of function stack frame (C implementation)](/img/a9/81644ee9ffb74a5dc8ff1bc3977f49.png)
[internal mental skill] - creation and destruction of function stack frame (C implementation)

猿辅导技术进化论:助力教与学 构想未来学校
随机推荐
Have you learned the common SQL interview questions on the short video platform?
Online sql to XML tool
Internal implementation principle of yymodel
How to simulate common web application operations when using testcafe
【ARXIV2203】SepViT: Separable Vision Transformer
数据安全逐步落地,必须紧盯泄露源头
Specific differences between typedef and define
HDU 2586 How far away ? (LCA multiplication method)
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!
[learning record] data enhancement 1
HashSet add
Reading notes of SMT practical guide 1
7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
FreeRTOS learning (I)
Check box error
Mysql基本查询
Visual studio 2019 new OpenGL project does not need to reconfigure the environment
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
Flink mind map
jsonp 单点登录 权限检验