当前位置:网站首页>MySQL date function
MySQL date function
2022-07-28 18:45:00 【web18224617243】
MySQL Date function
- 1、DATE()
- 2、TIME()
- 3、TIMESTAMP()
- 4、NOW()、CURRENT_TIMESTAMP、CURRENT_TIMESTAMP()、SYSDATE()
- 5、STR_TO_DATE()
- 6、DATE_FORMAT()
- 7、UNIX_TIMESTAMP()
- 8、FROM_UNIXTIME
- 9、 Other date functions
- 10、 Date format
1、DATE()
return date . Format :YYYY-MM-DD
SELECT DATE(NOW());
> 2022-04-04
2、TIME()
return date . Format :HH-mm-ss
SELECT TIME(NOW());
> 16:25:09
3、TIMESTAMP()
return Date time . Format :YYYY-MM-DD HH-mm-ss
SELECT TIMESTAMP(NOW());
> 2022-04-04 16:31:12
4、NOW()、CURRENT_TIMESTAMP、CURRENT_TIMESTAMP()、SYSDATE()
select NOW(),CURRENT_TIMESTAMP,CURRENT_TIMESTAMP();
> 2022-04-04 16:40:03 2022-04-04 16:40:03 2022-04-04 16:40:03
It should be noted that the first three are ** Returns the time when the statement execution started **, the latter **SYSDATE() Different Returns the time when this function was executed **
select SLEEP(1),NOW(),CURRENT_TIMESTAMP,CURRENT_TIMESTAMP(),SYSDATE();
> 0 2022-04-04 16:42:22 2022-04-04 16:42:22 2022-04-04 16:42:22 2022-04-04 16:42:23
Here we can see that we use sleep(1), Give Way sql Delay a second to get the first three and seelp(1) The difference between SYSDATE() Obvious ratio NOW() These are more than a second , So we can know The expected result is before and after the delay now() The time corresponding to the function does not change ,sysdate() The time of is equal to the previous time plus the delay time
5、STR_TO_DATE()
According to the specified format , Pass the string to the corresponding date or date type
example 1、
SELECT STR_TO_DATE('2022-04-04 22:50:17','%Y-%m-%d');
> 2022-04-04
When there are no hours, minutes and seconds, the display is ignored here
example 2、
SELECT STR_TO_DATE('2022-04-04 22:50:17','%Y-%m-%d %H');
> 2022-04-04 22:00:00
6、DATE_FORMAT()
Return the date as the corresponding string according to the specified format
More date formats
example 1、
SELECT DATE_FORMAT('2022-04-04 22:50:17','%Y-%m-%d');
> 2022-04-04
example 2、
SELECT DATE_FORMAT('2022-04-04 22:50:17','%Y-%m-%d %H');
> 2022-04-04 22
7、UNIX_TIMESTAMP()
Get the timestamp of the date (10 position )
SELECT unix_timestamp(now());
> 1649083817
8、FROM_UNIXTIME
According to time stamp (10 position ) Return date
example 1、
SELECT FROM_UNIXTIME(1649087115)
> 2022-04-04 23:45:15
example 2、 Add the second parameter format
SELECT FROM_UNIXTIME(1649087115,'%Y-%m-%s')
> 2022-04-15
9、 Other date functions
Function name
describe
Examples
result
NOW()
Get current date Format ’yyyy-MM-dd HH:mm:ss’
select NOW();
2022-04-04 22:50:17
CURDATE()
Get current date Format ’yyyy-MM-dd’
select CURDATE();
2022-04-04
CURTIME()
Get current date Format ’HH:mm:ss’
select CURTIME();
22:50:17
CURRENT_TIMESTAMP()
Get current date Format ’yyyy-MM-dd HH:mm:ss’
select CURRENT_TIMESTAMP();
2022-04-04 22:50:17
CURRENT_DATE()
Get current date Format ’yyyy-MM-dd’
select CURRENT_DATE();
2022-04-04
CURRENT_TIME()
Get current date Format ’HH:mm:ss’
select CURRENT_TIME();
22:50:17
CURRENT_TIMESTAMP
Get current date Format ’yyyy-MM-dd HH:mm:ss’
select CURRENT_TIMESTAMP;
2022-04-04 22:50:17
CURRENT_DATE
Get current date Format ’yyyy-MM-dd’
select CURRENT_DATE;
2022-04-04
CURRENT_TIME
Get current date Format ’HH:mm:ss’
select CURRENT_TIME;
22:50:17
SYSDATE()
Get the execution time of this function Format ’yyyy-MM-dd HH:mm:ss’
select SYSDATE;
2022-04-04 22:50:17
YEAR()
Get year
select YEAR(NOW()) ;
2022
MONTH()
Get month
select MONTH(NOW())
4
DAYOFMONTH()
Get the number of days in the month
select DAYOFMONTH(NOW())
4
DAYOFYEAR()
Get the number of days in the year
select DAYOFYEAR(NOW())
94
WEEKDAY()
The acquisition date is the day of the week (0- On behalf of Monday 、1- On behalf of Tuesday )
select WEEKDAY(NOW());
0
DAYOFWEEK()
The acquisition date is the day of the week (1- For Sunday 、2- On behalf of Monday )
select DAYOFWEEK(NOW());
2
WEEKOFYEAR()
The acquisition date is the week of the year
select WEEKOFYEAR(NOW());
14
TO_DAYS()
Get from 0000-00-00 Days to date
SELECT TO_DAYS(NOW());
738614
HOUR()
For hours
SELECT HOUR(NOW());
22
MINUTE()
Get minutes
SELECT MINUTE(NOW());
50
SECOND()
Get seconds
SELECT SECOND(NOW());
17
10、 Date format
Format
describe
%Y
year ,4 position
%y
year ,2 position
%M
month English representation January
%b
month English abbreviation means Apr
%m
month , The number (00-12)
%c
month , The number (0-12)
%d
Day of the month , The number (00-31)
%e
Day of the month , The number (0-31)
%j
Days of (001-366)
%H
Hours (00-23)
%I
Hours (01-12) 0 Time representative 12 1 Time representative 1
%k
Hours (0-23)
%i
minute , The number (00-59)
%S
second (00-59)
%s
second (00-59)
%f
Microsecond
%T
Time , 24- Hours (hh:mm:ss)
%r
Time ,12- Hours (hh:mm:ss AM or PM)
%p
AM or PM
%a
The abbreviation of the name of the week for example Mon
%W
Week name for example Monday
%w
Days of the week (0= Sunday , 6= Saturday )
%U
Zhou (00-53) Sunday is the first day of the week
%u
Zhou (00-53) Monday is the first day of the week
%V
Zhou (01-53) Sunday is the first day of the week , And %X Use
%v
Zhou (01-53) Monday is the first day of the week , And %x Use
%X
year , Sunday is the first day of the week ,4 position , And %V Use
%x
year , Monday is the first day of the week ,4 position , And %v Use
边栏推荐
- Multithreading and high concurrency -- source code analysis AQS principle
- 1.1. Sparse array
- Introduction to the principle of signal source
- NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!
- MYSQL入门与进阶(十)
- Detailed explanation of oscilloscope probe
- MySQL日期函数
- 2022-07-27 study notes of group 4 self-cultivation class (every day)
- Meta Q2 earnings: revenue fell for the first time, and metaverse will compete with apple
- Tencent Tang Daosheng: open source is a new mode of production and collaboration in the era of industrial Internet
猜你喜欢

Gateway入门

记录自己在厦门两年来的面试经历--完结篇

2022-07-27 study notes of group 4 self-cultivation class (every day)

顿悟!百度强推的Redis天花板笔记,原来数据库是这样理解的

SQL Server stuff and for XML path

Six countries in Europe and the United States launched an express line product to optimize the "end-to-end" performance service on the 5th

Brief introduction to the principle of spectrometer II

EasyNLP中文文图生成模型带你秒变艺术家

1.1. Sparse array

直播|StarRocks 技术内幕 :低基数全局字典优化
随机推荐
MYSQL入门与进阶(一)
Ue5 gas learning notes 1.8 game special effects (gameplaycue)
Introduction and advanced MySQL (4)
When golang encounters high concurrency seckill
1.1. Sparse array
Ue5 gas learning notes 0.1 case Preview
Brief introduction to the principle of spectrometer I
UE5 GAS 学习笔记 1.1能力系统组件Ability System Component
Docker builds MySQL master-slave replication
How to see the future development of software testing?
LeetCode_ 1137_ Nth teponacci number
jvm四种引用类型
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
MongoDB数据库复制表
redis优势以及数据结构相关知识
Random talk on GIS data (VI) - projection coordinate system
Ue5 gas learning notes 1.6 skills gameplay ability
2022.7.26 constructor, interview: the role of new, deep copy and shallow copy
LVS手册
Golang并发模型之