当前位置:网站首页>[MySQL] 15 super detailed MySQL date type function summary
[MySQL] 15 super detailed MySQL date type function summary
2022-07-21 21:17:00 【Zimujun】

MySQL Date type function directory
1. Get date 、 Time
function | effect |
---|---|
CURDATE(), CURRENT_DATE() | Return current date , Include only mm / DD / yyyy |
CURTIME() | Return current time , Only hours, minutes, seconds |
NOW() / SYSDATE() / CURRENT_TIMESTAMP() / LOCALTIME() / LOCALTIMESTAMP() | Returns the current system date and time |
UTC_DATE() | return UTC( World standard time ) date |
UTC_TIME() | return UTC( World standard time ) Time |
2. Date and time stamp conversion
function | effect |
---|---|
UNIX_TIMESTAMP() | With UNIX Returns the current time in the form of a timestamp .SELECT UNIX_TIMESTAMP() ->1634348884 |
UNIX_TIMESTAMP(date) | Time date With UNIX Return in the form of a timestamp . |
FROM_UNIXTIME(timestamp) | take UNIX Time stamp converted to normal format |
3. Get month 、 week 、 Weeks 、 Functions such as days
function | effect |
---|---|
YEAR(date) / MONTH(date) / DAY(date) | Return the specific date value |
HOUR(time) / MINUTE(time) / SECOND(time) | Return the specific time value |
MONTHNAME(date) | Return to the month :January |
DAYNAME(date) | Day of the week :MONDAY |
WEEKDAY | What day of the week : Monday is 0, It was on Tuesday 1,…, Sunday is 6 |
QUARTER(date) | Returns the quarter corresponding to the date , Range 1~4 |
WEEK(date), WEEKOFYEAR(date) | Go back to the week of the year |
DAYOFYEAR(date) | The return date is the day of the year |
DAYOFMONTH(date) | The return date is the day of the month |
DAYOFWEEK(date) | What day of the week , Be careful : Sunday is 1,…, Saturday is 7 |
4. Operation function of date
function | effect |
---|---|
EXTRACT(typr FROM date) | Returns a specific part of a specified date ,type Specifies the value to be returned |
EXTRACT(typr FROM date) Function type
The value and meaning of :

5. Time and second conversion function
function | effect |
---|---|
TIME_TO_SEC(time) | take time Convert to seconds and return the result value . Change the formula to : Hours ×3600+ minute ×60+ second |
SEC_TO_TIME(seconds) | take seconds Convert to time including hours, minutes and seconds |
6. A function that calculates the date and time
function | effect |
---|---|
DATE_ADD(datatime, INTERVAL expr type), ADDDATE(date, INTERVAL expr type) | Returns the difference between the given date and time INTERBAL Date time of the time period |
DATE_SUB(date, INTERVAL expr type), SUBDATE(date, INTERVAL expr type) | Return and date Difference between INTERVAL The date of the interval |
In the above functions type
The value of :

The first 2 Group :
function | effect |
---|---|
ADDTIME(time1, time2) | return time1 add time2 Time for . When time2 When it is a number , It stands for seconds , Can be negative |
SUBTIME(time1, time2) | return time1 subtract time2 After time . When time2 When it is a number , It stands for seconds , Can be negative |
DATEDIFF(date1, date2) | return date1-date2 Number of days between dates |
TIMEDIFF(time1, time2) | return time1-time2 Time interval of |
FROM_DAYS(N) | Return from 0000 year 1 month 1 The date of ,N Days later |
TO_DAYS(date) | Return date date distance 0000 year 1 month 1 Days of the day |
LAST_DAY(date) | return date The date of the last day of the month |
MAKEDATE(year, n) | Returns a date for a given year and the number of days in the year |
MAKETIME(hour, minute, second) | Combine the given hours, minutes and seconds into time and return |
PERIOD_ADD(time, n) | return time add n After time |
7. Formatting and parsing of dates
function | effect |
---|---|
DATE_FORMAT(date, fmt) | According to the string fmt Format date date value |
TIME_FORMAT(time, fmt) | According to the string fmt Format time time value |
GET_FORMAT(date_type, format_type) | Returns the display format of the date string |
STR_TO_DATE(str, fmt) | According to the string fmt Yes str To analyze , Resolve to a date |
Above Not GET_FORMAT Function fmt Common format characters for parameters :
Format symbol | explain | Format symbol | explain |
---|---|---|---|
%Y | 4 The number of digits indicates the year | %y | 2 The number of digits indicates the year |
%M | The name of the month means the month (January,…) | %m | 2 Digits indicate months (01, …) |
%b | Abbreviated month name (Jan., Feb., …) | %c | The number represents the month (1, 2, …) |
%D | The English suffix indicates the number of days in the month (1st, 2nd, …) | %d | 2 Digits indicate the number of days in the month (01, 02, …) |
%e | The number of days in the month (1, 2, …) | ||
%H | 2 Digits indicate hours ,24 hourly (01, …, 24) | %h or %l | 2 Digits indicate hours ,12 hourly |
%k | The number represents the hour ,24 hourly (1, 2, …) | %l | The number represents the hour ,12 hourly (1, 2, …) |
%i | 2 Digits indicate minutes (01, …) | %S or %s | 2 Digits indicate seconds (01, …, 60) |
%W | The name of the week of the week (Sunday, …) | %a | Abbreviation of week in a week (Sun,…, Mon.) |
%w | The number of days in the week (0=Sunday, 1=Monday, …) | ||
%j | 3 The number of digits represents the number of days in the year (001, …365) | %U | The numerical form represents the week of the year (1, 2, …) among Sunday For the first day of the week |
%u | The numerical form represents the week of the year (1, 2, …) among Monday For the first day of the week | ||
%T | 24 hourly | %r | 12 hourly |
%p | AM or PM | %% | Express % |
GET_FORMAT Function date_type
and format_type
The parameter values are as follows :
The date type | Format type | Format string returned |
---|---|---|
DATE | USA | %m.%d.%Y |
DATE | JIS | %Y-%m-%d |
DATE | ISO | %Y-%m-%d |
DATE | EUR | %d.%m.%Y |
DATE | INTERNAL | %Y%m%d |
TIME | USA | %h:%i:%s %p |
TIME | JIS | %H:%i:%s |
TIME | ISO | %H:%i:%s |
TIME | EUR | %H.%i.%s |
TIME | INTERNAL | %H%i%s |
DATETIME | USA | %Y-%m-%d %H.%i.%s |
DATETIME | JIS | %Y-%m-%d %H:%i:%s |
DATETIME | ISO | %Y-%m-%d %H:%i:%s |
DATETIME | EUR | %Y-%m-%d %H.%i.%s |
DATETIME | INTERNAL | %Y%m%d%H%i%s |
边栏推荐
- 【MySQL】15-超详细MySQL日期类型函数总结
- 非让写标题,烦死惹
- 上榜 | Navicat 入选信通院发布《全球数据库产业图谱》
- Redis (III) - common redis commands
- VMware出现“该虚拟机似乎正在使用中”问题
- [MySQL] detailed summary of MySQL internal and external connections
- Oracle RAC镜像恢复的单实例数据库Redo日志增量抽取报错: ORA-01291 & 删除日志组报错: ORA-01567
- 我的创作纪念日
- (Sword finger off version) rotate the array to find the minimum value (easy to understand)
- 如何在执行前测试 Insert 和 Update 语句
猜你喜欢
Oracle 分组数据
【PyTorch教程】07-PyTorch如何使用多块GPU训练神经网络模型
Wu Enda deep learning l4w4 face recognition
Concurrent programming (XXVI) -reentrantlock application
[pytorch tutorial] 01- how to create and initialize pytoch tensor (latest in 2022)
Amy Tabb robot world hand eye calibration (2. Experimental results)
并发编程(二十七) - JUC之原子类
使用 MySQL 慢速查询日志
Concurrent programming (XXIX) - memory layout of objects
【MySQL】17-超详细的MySQL聚合函数总结
随机推荐
Character function and string function and their simulation implementation (C language)
[MySQL] detailed summary of MySQL internal and external connections
C language 2022 Shanxi upgraded C language knowledge points
并发编程(二十八) - LongAdder原理
【MySQL】15-超详细MySQL日期类型函数总结
【PyTorch教程】05-如何使用PyTorch训练神经网络模型 (2022年最新)
【PyTorch教程】06-如何使用PyTorch搭建神经网络模型并进行训练
Azure数据仓库表中的数据经常使用的三种分布策略(hash、round_robin 或 replicated)简介
【PyTorch教程】04-详解torchvision 0.13中的预训练模型加载的更新及报错的解决方法 (2022年最新)
boost::this_ Thread:: sleep (boost:: posix_time:: microseconds (100000)) reports an error "this_thread": "the symbol on the left side of":: "must be of a type
【MySQL】MySQL中多表连接的背景、笛卡尔积的错误与如何正确地多表查询
【MySQL】MySQL的自然连接和USING连接详细总结
实操演练 | 将 MySQL 表导出到 CSV
(note) Wu Enda's in-depth study l4w2
Concurrent programming (XXVI) -reentrantlock application
Concurrent programming (XXVII) - Atomic classes of JUC
Open3d official website code learning
并发编程(二十)-ReentrantLock 加锁、解锁原理
并发编程(三十一) - ReetrantReadWriteLock 读写锁原理
PostgreSQL新建用户登录报错:FATAL: Peer authentication failed for user “test“