MySQL 的日期和時間函式

lhrbest發表於2018-04-16

MySQL 的日期和時間函式





官網:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date





   

MySQL的日期函式較多,只需要掌握常用的即可,常用的日期或時間函式參考如下的表格:

 STYLEREF 1 \s 4- SEQ \* ARABIC \s 1 1 MySQL的日期函式

函式

函式功能描述

函式舉例

DAYOFWEEK(DATE)

返回DATE的星期索引(1= Sunday2= Monday... 7=Saturday

mysql> SELECT DAYOFWEEK('2016-05-24');

+-------------------------+

| DAYOFWEEK('2016-05-24') |

+-------------------------+

|                       3 |

+-------------------------+

DAYOFYEAR(DATE)

返回DATE是一年中的第幾天,範圍為1366

mysql>  SELECT DAYOFYEAR('2016-05-24');

+-------------------------+

| DAYOFYEAR('2016-05-24') |

+-------------------------+

|                     145 |

+-------------------------+

HOUR(TIME)/MINUTE(TIME)/SECOND(TIME)

返回TIME的小時值/分鐘值/秒值,範圍為023

mysql> SELECT HOUR('10:05:03'),MINUTE('10:05:03'),SECOND('10:05:03');

+------------------+--------------------+--------------------+

| HOUR('10:05:03') | MINUTE('10:05:03') | SECOND('10:05:03') |

+------------------+--------------------+--------------------+

|               10 |                  5 |                  3 |

+------------------+--------------------+--------------------+

DATE_FORMAT(DATE,FORMAT)

依照FORMAT字串格式化DATE值,修飾符的含義:

%M:月的名字(January..December

%W:星期的名字(Sunday..Saturday

%D:有英文字尾的某月的第幾天(0th1st2nd3rd等)

 

%Y4位數字年份

%y2位數字年份

 

%m:月,數字(00..12

%c:月,數字(0..12

 

%d:代表月份中的天數,格式為(00……31

%e:代表月份中的天數,格式為(0……31

 

 

%x:周值的年份,星期一是一個星期的第一天,數字的,4位,與“%v”一同使用

%a:縮寫的星期名(Sun..Sat

 

%H:小時(00..23

%k:小時(0..23

%h:小時(01..12

%I:小時(01..12

%l:小時(1..12

 

%i:代表分鐘, 格式為(00……59) 。只有這一個代表分鐘,大寫的I不代表分鐘代表小時

 

%r:代表 時間,格式為12 小時(hh:mm:ss [AP]M)

%T:代表 時間,格式為24 小時(hh:mm:ss

 

%S:秒(00..59

%s:秒(00..59

 

%pAMPM

%w:一週中的天數(0=Sunday..6=Saturday

mysql> SELECT DATE_FORMAT('2016-05-24', '%W %M %Y');

+---------------------------------------+

| DATE_FORMAT('2016-05-24', '%W %M %Y') |

+---------------------------------------+

| Tuesday May 2016                      |

+---------------------------------------+

STR_TO_DATE()

將字串轉換為日期型別

mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');

+---------------------------------------+

| STR_TO_DATE('04/31/2004', '%m/%d/%Y') |

+---------------------------------------+

| 2004-04-31                            |

+---------------------------------------+

1 row in set (0.00 sec)

CURDATE()/CURRENT_DATE

以“YYYY-MM-DD”或“YYYYMMDD”格式返回當前的日期值

mysql> SELECT CURDATE(),CURRENT_DATE;

+------------+--------------+

| CURDATE()  | CURRENT_DATE |

+------------+--------------+

| 2017-07-28 | 2017-07-28   |

+------------+--------------+

CURTIME()

/CURRENT_TIME

以“HH:MM:SS”或“HHMMSS”格式返回當前的時間值

mysql> SELECT CURTIME(),CURRENT_TIME();

+-----------+----------------+

| CURTIME() | CURRENT_TIME() |

+-----------+----------------+

| 16:05:37  | 16:05:37       |

+-----------+----------------+

NOW()/SYSDATE()

/CURRENT_TIMESTAMP

以“YYYY-MM-DD HH:MM:SS”或“YYYYMMDDHHMMSS”格式返回當前的日期時間值

mysql> SELECT NOW(),SYSDATE(),CURRENT_TIMESTAMP;

+---------------------+---------------------+---------------------+

| NOW()               | SYSDATE()           | CURRENT_TIMESTAMP   |

+---------------------+---------------------+---------------------+

| 2017-07-28 16:04:31 | 2017-07-28 16:04:31 | 2017-07-28 16:04:31 |

+---------------------+---------------------+---------------------+

SEC_TO_TIME(NUMBER)

以“HH:MM:SS”或“HHMMSS”格式返回入參值被轉換到時分秒後的值

mysql> SELECT SEC_TO_TIME(2378);

+-------------------+

| SEC_TO_TIME(2378) |

+-------------------+

| 00:39:38          |

+-------------------+

TIME_TO_SEC(TIME)

將引數TIME轉換為秒數後返回

mysql> SELECT TIME_TO_SEC('22:23:00');

+-------------------------+

| TIME_TO_SEC('22:23:00') |

+-------------------------+

|                   80580 |

+-------------------------+

其它的函式請查閱官方文件。

真題1、MySQL中的字串和日期相互轉化的函式是什麼?

答案:MySQL中日期轉換為字串使用DATE_FORMAT函式,相當於Oracle中的TO_CHAR函式,而將字串轉換為日期格式,使用的函式為STR_TO_DATE,相當於Oracle中的TO_DATE函式。

STR_TO_DATE函式的使用示例如下所示:

select str_to_date('09/01/2009','%m/%d/%Y');

select str_to_date('20140422154706','%Y%m%d%H%i%s');

select str_to_date('2014-04-22 15:47:06','%Y-%m-%d %H:%i:%s');

 





12.7 Date and Time Functions

This section describes the functions that can be used to manipulate temporal values. See Section 11.3, “Date and Time Types”, for a description of the range of values each date and time type has and the valid formats in which values may be specified.

Table 12.13 Date and Time Functions

Name Description
ADDDATE() Add time values (intervals) to a date value
ADDTIME() Add time
CONVERT_TZ() Convert from one time zone to another
CURDATE() Return the current date
CURRENT_DATE(), CURRENT_DATE Synonyms for CURDATE()
CURRENT_TIME(), CURRENT_TIME Synonyms for CURTIME()
CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP Synonyms for NOW()
CURTIME() Return the current time
DATE() Extract the date part of a date or datetime expression
DATE_ADD() Add time values (intervals) to a date value
DATE_FORMAT() Format date as specified
DATE_SUB() Subtract a time value (interval) from a date
DATEDIFF() Subtract two dates
DAY() Synonym for DAYOFMONTH()
DAYNAME() Return the name of the weekday
DAYOFMONTH() Return the day of the month (0-31)
DAYOFWEEK() Return the weekday index of the argument
DAYOFYEAR() Return the day of the year (1-366)
EXTRACT() Extract part of a date
FROM_DAYS() Convert a day number to a date
FROM_UNIXTIME() Format Unix timestamp as a date
GET_FORMAT() Return a date format string
HOUR() Extract the hour
LAST_DAY Return the last day of the month for the argument
LOCALTIME(), LOCALTIME Synonym for NOW()
LOCALTIMESTAMP, LOCALTIMESTAMP() Synonym for NOW()
MAKEDATE() Create a date from the year and day of year
MAKETIME() Create time from hour, minute, second
MICROSECOND() Return the microseconds from argument
MINUTE() Return the minute from the argument
MONTH() Return the month from the date passed
MONTHNAME() Return the name of the month
NOW() Return the current date and time
PERIOD_ADD() Add a period to a year-month
PERIOD_DIFF() Return the number of months between periods
QUARTER() Return the quarter from a date argument
SEC_TO_TIME() Converts seconds to 'HH:MM:SS' format
SECOND() Return the second (0-59)
STR_TO_DATE() Convert a string to a date
SUBDATE() Synonym for DATE_SUB() when invoked with three arguments
SUBTIME() Subtract times
SYSDATE() Return the time at which the function executes
TIME() Extract the time portion of the expression passed
TIME_FORMAT() Format as time
TIME_TO_SEC() Return the argument converted to seconds
TIMEDIFF() Subtract time
TIMESTAMP() With a single argument, this function returns the date or datetime expression; with two arguments, the sum of the arguments
TIMESTAMPADD() Add an interval to a datetime expression
TIMESTAMPDIFF() Subtract an interval from a datetime expression
TO_DAYS() Return the date argument converted to days
TO_SECONDS() Return the date or datetime argument converted to seconds since Year 0
UNIX_TIMESTAMP() Return a Unix timestamp
UTC_DATE() Return the current UTC date
UTC_TIME() Return the current UTC time
UTC_TIMESTAMP() Return the current UTC date and time
WEEK() Return the week number
WEEKDAY() Return the weekday index
WEEKOFYEAR() Return the calendar week of the date (1-53)
YEAR() Return the year
YEARWEEK() Return the year and week
Name Description






About Me

.............................................................................................................................................

● 本文作者:小麥苗,部分內容整理自網路,若有侵權請聯絡小麥苗刪除

● 本文在itpub(http://blog.itpub.net/26736162/abstract/1/)、部落格園(http://www.cnblogs.com/lhrbest)和個人微信公眾號(xiaomaimiaolhr)上有同步更新

● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/

● 本文部落格園地址:http://www.cnblogs.com/lhrbest

● 本文pdf版、個人簡介及小麥苗雲盤地址:http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答:http://blog.itpub.net/26736162/viewspace-2134706/

● DBA寶典今日頭條號地址:http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

.............................................................................................................................................

● QQ群號:230161599(滿)、618766405

● 微信群:可加我微信,我拉大家進群,非誠勿擾

● 聯絡我請加QQ好友646634621,註明新增緣由

● 於 2018-04-01 06:00 ~ 2018-04-31 24:00 在魔都完成

● 最新修改時間:2018-04-01 06:00 ~ 2018-04-31 24:00

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

.............................................................................................................................................

小麥苗的微店https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

小麥苗出版的資料庫類叢書http://blog.itpub.net/26736162/viewspace-2142121/

小麥苗OCP、OCM、高可用網路班http://blog.itpub.net/26736162/viewspace-2148098/

.............................................................................................................................................

使用微信客戶端掃描下面的二維碼來關注小麥苗的微信公眾號(xiaomaimiaolhr)及QQ群(DBA寶典),學習最實用的資料庫技術。

小麥苗的微信公眾號小麥苗的DBA寶典QQ群2《DBA筆試面寶典》讀者群小麥苗的微店

   小麥苗的微信公眾號      小麥苗的DBA寶典QQ群2     《DBA筆試面試寶典》讀者群       小麥苗的微店

.............................................................................................................................................

MySQL 的日期和時間函式
DBA筆試面試講解群
《DBA寶典》讀者群 歡迎與我聯絡



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2152970/,如需轉載,請註明出處,否則將追究法律責任。

相關文章