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

边栏推荐
- Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"
- Mysql基本查询
- Keil Chinese garbled code solution
- 分享几种管理C程序中标志位的方法
- HDU 2874 connections between cities
- HDU 3585 maximum shortest distance
- 为什么md5不可逆,却还可能被md5免费解密网站解密
- After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
- Improve the core quality of steam education among students
- HDU 3592 World Exhibition (differential constraint)
猜你喜欢

Table image extraction based on traditional intersection method and Tesseract OCR

flink思维导图

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

Duoyu security browser will improve the security mode and make users browse more safely

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!

Method of converting UI file to py file

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?

7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格

Observable time series data downsampling practice in Prometheus

The go zero singleton service uses generics to simplify the registration of handler routes
随机推荐
【ARXIV2204】Simple Baselines for Image Restoration
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
go-zero单体服务使用泛型简化注册Handler路由
Making RPM packages with nfpm
HDU 3585 maximum shortest distance
Anaconda common instructions
Win10 machine learning environment construction pycharm, anaconda, pytorch
What is the core value of testing?
POJ 1330 Nearest Common Ancestors (lca)
Introduction to testcafe
Specific differences between typedef and define
HDU 2586 How far away ? (LCA multiplication method)
What should testers know about login security?
Comprehensively analyze the differences between steam and maker Education
The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
Gan: generative advantageous nets -- paper analysis and the mathematical concepts behind it
Research on the design of robot education in stem course
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?
7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
jsonp 单点登录 权限检验