SQL - 日期函数
-
简述
下表列出了通过 SQL 可用的所有重要的日期和时间相关函数。您的 RDBMS 还支持各种其他功能。给定的列表基于 MySQL RDBMS。序号 功能说明 1 ADDDATE() 添加日期2 ADDTIME() 增加时间3 CONVERT_TZ() 从一个时区转换到另一个时区4 CURDATE() 返回当前日期5 CURRENT_DATE(), CURRENT_DATE CURDATE() 的同义词6 CURRENT_TIME(), CURRENT_TIME CURTIME() 的同义词7 CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP NOW() 的同义词8 CURTIME() 返回当前时间9 DATE_ADD() 添加两个日期10 DATE_FORMAT() 按指定格式日期11 DATE_SUB() 减去两个日期12 DATE() 提取日期或日期时间表达式的日期部分13 DATEDIFF() 减去两个日期14 DAY() DAYOFMONTH() 的同义词15 DAYNAME() 返回工作日的名称16 DAYOFMONTH() 返回月份中的第几天 (1-31)17 DAYOFWEEK() 返回参数的工作日索引18 DAYOFYEAR() 返回一年中的第几天 (1-366)19 EXTRACT 提取日期的一部分20 FROM_DAYS() 将天数转换为日期21 FROM_UNIXTIME() 将日期格式化为 UNIX 时间戳22 HOUR() 提取小时23 LAST_DAY 返回参数的月份的最后一天24 LOCALTIME(), LOCALTIME 现在()的同义词25 LOCALTIMESTAMP, LOCALTIMESTAMP() 现在()的同义词26 MAKEDATE() 根据年份和日期创建日期27 MAKETIME 找时间 ()28 MICROSECOND() 从参数返回微秒29 MINUTE() 从参数返回分钟30 MONTH() 从过去的日期返回月份31 MONTHNAME() 返回月份的名称32 NOW() 返回当前日期和时间33 PERIOD_ADD() 将期间添加到年月34 PERIOD_DIFF() 返回期间之间的月数35 QUARTER() 从日期参数返回季度36 SEC_TO_TIME() 将秒转换为 'HH:MM:SS' 格式37 SECOND() 返回第二个 (0-59)38 STR_TO_DATE() 将字符串转换为日期39 SUBDATE() 当使用三个参数调用时,DATE_SUB() 的同义词40 SUBTIME() 减去时间41 SYSDATE() 返回函数执行的时间42 TIME_FORMAT() 格式为时间43 TIME_TO_SEC() 返回转换为秒的参数44 TIME() 提取传递的表达式的时间部分45 TIMEDIFF() 减去时间46 TIMESTAMP() 此函数使用单个参数返回日期或日期时间表达式。有两个参数,参数的总和47 TIMESTAMPADD() 向日期时间表达式添加间隔48 TIMESTAMPDIFF() 从日期时间表达式中减去一个间隔49 TO_DAYS() 返回转换为天的日期参数50 UNIX_TIMESTAMP() 返回一个 UNIX 时间戳51 UTC_DATE() 返回当前 UTC 日期52 UTC_TIME() 返回当前 UTC 时间53 UTC_TIMESTAMP() 返回当前 UTC 日期和时间54 WEEK() 返回周数55 WEEKDAY() 返回工作日索引56 WEEKOFYEAR() 返回日期的日历周 (1-53)57 YEAR() 返回年份58 YEARWEEK() 返回年份和星期 -
ADDDATE(date,INTERVAL expr unit), ADDDATE(expr,days)
当使用第二个参数的 INTERVAL 形式调用时,ADDDATE() 是 DATE_ADD() 的同义词。相关函数 SUBDATE() 是 DATE_SUB() 的同义词。有关 INTERVAL 单位参数的信息,请参阅 DATE_ADD() 的讨论。mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY); +---------------------------------------------------------+ | DATE_ADD('1998-01-02', INTERVAL 31 DAY) | +---------------------------------------------------------+ | 1998-02-02 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT ADDDATE('1998-01-02', INTERVAL 31 DAY); +---------------------------------------------------------+ | ADDDATE('1998-01-02', INTERVAL 31 DAY) | +---------------------------------------------------------+ | 1998-02-02 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
当使用第二个参数的天数形式调用时,MySQL 将其视为要添加到 expr 的整数天数。mysql> SELECT ADDDATE('1998-01-02', 31); +---------------------------------------------------------+ | DATE_ADD('1998-01-02', INTERVAL 31 DAY) | +---------------------------------------------------------+ | 1998-02-02 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
ADDTIME(expr1,expr2)
ADDTIME() 将 expr2 添加到 expr1 并返回结果。expr1 是时间或日期时间表达式,而 expr2 是时间表达式。mysql> SELECT ADDTIME('1997-12-31 23:59:59.999999','1 1:1:1.000002'); +---------------------------------------------------------+ | DATE_ADD('1997-12-31 23:59:59.999999','1 1:1:1.000002') | +---------------------------------------------------------+ | 1998-01-02 01:01:01.000001 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
CONVERT_TZ(dt,from_tz,to_tz)
这会将日期时间值 dt 从 from_tz 给定的时区转换为 to_tz 给定的时区,并返回结果值。如果参数无效,此函数返回 NULL。mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET'); +---------------------------------------------------------+ | CONVERT_TZ('2004-01-01 12:00:00','GMT','MET') | +---------------------------------------------------------+ | 2004-01-01 13:00:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00'); +---------------------------------------------------------+ | CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00') | +---------------------------------------------------------+ | 2004-01-01 22:00:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
CURDATE()
返回当前日期作为 'YYYY-MM-DD' 或 YYYYMMDD 格式的值,具体取决于函数是在字符串还是数字上下文中使用。mysql> SELECT CURDATE(); +---------------------------------------------------------+ | CURDATE() | +---------------------------------------------------------+ | 1997-12-15 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT CURDATE() + 0; +---------------------------------------------------------+ | CURDATE() + 0 | +---------------------------------------------------------+ | 19971215 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
-
CURTIME()
以 'HH:MM:SS' 或 HHMMSS 格式返回当前时间,具体取决于函数是在字符串还是数字上下文中使用。该值以当前时区表示。mysql> SELECT CURTIME(); +---------------------------------------------------------+ | CURTIME() | +---------------------------------------------------------+ | 23:50:26 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT CURTIME() + 0; +---------------------------------------------------------+ | CURTIME() + 0 | +---------------------------------------------------------+ | 235026 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
-
-
DATE(expr)
提取日期或日期时间表达式 expr 的日期部分。mysql> SELECT DATE('2003-12-31 01:02:03'); +---------------------------------------------------------+ | DATE('2003-12-31 01:02:03') | +---------------------------------------------------------+ | 2003-12-31 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
DATEDIFF(expr1,expr2)
DATEDIFF() 返回 expr1 。expr2 表示为从一个日期到另一个日期的天数。expr1 和 expr2 都是日期或日期和时间表达式。计算中仅使用值的日期部分。mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30'); +---------------------------------------------------------+ | DATEDIFF('1997-12-31 23:59:59','1997-12-30') | +---------------------------------------------------------+ | 1 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
这些函数执行日期算术。这date是指定开始日期的 DATETIME 或 DATE 值。这expr is an expression specifying the interval value to be added or subtracted from the starting date. The expr is a string; it may start with a '-' for negative intervals.A unit is a keyword indicating the units in which the expression should be interpreted.The INTERVAL keyword and the unit specifier are not case sensitive.The following table shows the expected form of the expr argument for each unit value.unit Value Expected exprFormat MICROSECOND MICROSECONDS SECOND SECONDS MINUTE MINUTES HOUR HOURS DAY DAYS WEEK WEEKS MONTH MONTHS QUARTER QUARTERS YEAR YEARS SECOND_MICROSECOND 'SECONDS.MICROSECONDS' MINUTE_MICROSECOND 'MINUTES.MICROSECONDS' MINUTE_SECOND 'MINUTES:SECONDS' HOUR_MICROSECOND 'HOURS.MICROSECONDS' HOUR_SECOND 'HOURS:MINUTES:SECONDS' HOUR_MINUTE 'HOURS:MINUTES' DAY_MICROSECOND 'DAYS.MICROSECONDS' DAY_SECOND '天时:分:秒' DAY_MINUTE '天小时:分钟' DAY_HOUR '天小时' YEAR_MONTH '年月' 价值QUARTER和WEEK可从 MySQL 5.0.0 获得。版本。mysql> SELECT DATE_ADD('1997-12-31 23:59:59', -> INTERVAL '1:1' MINUTE_SECOND); +---------------------------------------------------------+ | DATE_ADD('1997-12-31 23:59:59', INTERVAL... | +---------------------------------------------------------+ | 1998-01-01 00:01:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT DATE_ADD('1999-01-01', INTERVAL 1 HOUR); +---------------------------------------------------------+ | DATE_ADD('1999-01-01', INTERVAL 1 HOUR) | +---------------------------------------------------------+ | 1999-01-01 01:00:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
DATE_FORMAT(date,format)
此命令根据格式字符串格式化日期值。以下说明符可用于格式字符串。在格式说明符字符之前需要 '%' 字符。序号 说明符和说明 1 %a工作日的缩写名称 (Sun..Sat)2 %b缩写月份名称(Jan..Dec)3 %c月份,数字 (0..12)4 %D带有英文后缀(0th, 1st, 2nd, 3rd, .)的月份日期5 %d日期,数字 (00..31)6 %e日期,数字 (0..31)7 %f微秒 (000000..999999)8 %H小时 (00..23)9 %h小时 (01..12)10 %I小时 (01..12)11 %i分钟,数字 (00..59)12 %j一年中的某一天 (001..366)13 %k小时 (0..23)14 %l小时 (1..12)15 %M月份名称(一月..十二月)16 %m月份,数字 (00..12)17 %p上午或下午18 %r时间,12 小时(hh:mm:ss 后跟 AM 或 PM)19 %S秒 (00..59)20 %s秒 (00..59)21 %T时间,24 小时制 (hh:mm:ss)22 %U周 (00..53),其中星期日是一周的第一天23 %u周 (00..53),其中星期一是一周的第一天24 %V周 (01..53),其中星期日是一周的第一天;与 %X 一起使用25 %v周 (01..53),其中星期一是一周的第一天;与 %x 一起使用26 %W工作日名称(星期日..星期六)27 %w星期几(0=星期日..6=星期六)28 %X星期天是一周的第一天的一周年,数字,四位数字;与 %V 一起使用29 %x一周的年份,其中星期一是一周的第一天,数字,四位数字;与 %v 一起使用30 %Y年份,数字,四位数31 %y年份,数字(两位数)32 %%文字 .%。特点33 %xx,对于任何.x。上面没有列出mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y'); +---------------------------------------------------------+ | DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y') | +---------------------------------------------------------+ | Saturday October 1997 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00' -> '%H %k %I %r %T %S %w'); +---------------------------------------------------------+ | DATE_FORMAT('1997-10-04 22:23:00....... | +---------------------------------------------------------+ | 22 22 10 10:23:00 PM 22:23:00 00 6 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
-
-
DAYNAME(date)
Returns the name of the weekday for date.mysql> SELECT DAYNAME('1998-02-05'); +---------------------------------------------------------+ | DAYNAME('1998-02-05') | +---------------------------------------------------------+ | Thursday | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
DAYOFMONTH(date)
Returns the day of the month for date, in the range 0 to 31.mysql> SELECT DAYOFMONTH('1998-02-03'); +---------------------------------------------------------+ | DAYOFMONTH('1998-02-03') | +---------------------------------------------------------+ | 3 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
DAYOFWEEK(date)
Returns the weekday index for date (1 = Sunday, 2 = Monday, ., 7 = Saturday). These index values correspond to the ODBC standard.mysql> SELECT DAYOFWEEK('1998-02-03'); +---------------------------------------------------------+ |DAYOFWEEK('1998-02-03') | +---------------------------------------------------------+ | 3 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
DAYOFYEAR(date)
Returns the day of the year for date, in the range 1 to 366.mysql> SELECT DAYOFYEAR('1998-02-03'); +---------------------------------------------------------+ | DAYOFYEAR('1998-02-03') | +---------------------------------------------------------+ | 34 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
EXTRACT(unit FROM date)
The EXTRACT() function uses the same kinds of unit specifiers as DATE_ADD() or DATE_SUB(), but extracts parts from the date rather than performing date arithmetic.mysql> SELECT EXTRACT(YEAR FROM '1999-07-02'); +---------------------------------------------------------+ | EXTRACT(YEAR FROM '1999-07-02') | +---------------------------------------------------------+ | 1999 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03'); +---------------------------------------------------------+ | EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03') | +---------------------------------------------------------+ | 199907 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
FROM_DAYS(N)
Given a day number N, returns a DATE value.mysql> SELECT FROM_DAYS(729669); +---------------------------------------------------------+ | FROM_DAYS(729669) | +---------------------------------------------------------+ | 1997-10-07 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
注意 − Use FROM_DAYS() with caution on old dates. It is not intended for use with values that precede the advent of the Gregorian calendar (1582). -
FROM_UNIXTIME(unix_timestamp)
-
FROM_UNIXTIME(unix_timestamp,format)
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or in a numeric context. The value is expressed in the current time zone. The unix_timestamp argument is an internal timestamp values, which are produced by the UNIX_TIMESTAMP() function.If the format is given, the result is formatted according to the format string, which is used in the same way as is listed in the entry for the DATE_FORMAT() function.mysql> SELECT FROM_UNIXTIME(875996580); +---------------------------------------------------------+ | FROM_UNIXTIME(875996580) | +---------------------------------------------------------+ | 1997-10-04 22:23:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
HOUR(time)
Returns the hour for time. The range of the return value is 0 to 23 for time-of-day values. However, the range of TIME values actually is much larger, so HOUR can return values greater than 23.mysql> SELECT HOUR('10:05:03'); +---------------------------------------------------------+ | HOUR('10:05:03') | +---------------------------------------------------------+ | 10 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
LAST_DAY(date)
Takes a date or datetime value and returns the corresponding value for the last day of the month. Returns NULL if the argument is invalid.mysql> SELECT LAST_DAY('2003-02-05'); +---------------------------------------------------------+ | LAST_DAY('2003-02-05') | +---------------------------------------------------------+ | 2003-02-28 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
-
-
MAKEDATE(year,dayofyear)
返回一个日期,给定年份和年份值。dayofyear 值必须大于 0,否则结果将为 NULL。mysql> SELECT MAKEDATE(2001,31), MAKEDATE(2001,32); +---------------------------------------------------------+ | MAKEDATE(2001,31), MAKEDATE(2001,32) | +---------------------------------------------------------+ | '2001-01-31', '2001-02-01' | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
MAKETIME(hour,minute,second)
返回根据小时、分钟和秒参数计算的时间值。mysql> SELECT MAKETIME(12,15,30); +---------------------------------------------------------+ | MAKETIME(12,15,30) | +---------------------------------------------------------+ | '12:15:30' | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
MICROSECOND(expr)
将时间或日期时间表达式 (expr) 的微秒作为 0 到 999999 范围内的数字返回。mysql> SELECT MICROSECOND('12:00:00.123456'); +---------------------------------------------------------+ | MICROSECOND('12:00:00.123456') | +---------------------------------------------------------+ | 123456 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
MINUTE(time)
返回时间的分钟,范围为 0 到 59。mysql> SELECT MINUTE('98-02-03 10:05:03'); +---------------------------------------------------------+ | MINUTE('98-02-03 10:05:03') | +---------------------------------------------------------+ | 5 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
-
MONTHNAME(date)
返回日期的月份全名。mysql> SELECT MONTHNAME('1998-02-05'); +---------------------------------------------------------+ | MONTHNAME('1998-02-05') | +---------------------------------------------------------+ | February | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
NOW()
将当前日期和时间作为 'YYYY-MM-DD HH:MM:SS' 或 YYYYMMDDHHMMSS 格式的值返回,具体取决于函数是在字符串还是数字上下文中使用。该值以当前时区表示。mysql> SELECT NOW(); +---------------------------------------------------------+ | NOW() | +---------------------------------------------------------+ | 1997-12-15 23:50:26 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
PERIOD_ADD(P,N)
将 N 个月添加到期间 P(格式为 YYMM 或 YYYYMM)。返回格式为 YYYYMM 的值。请注意,期间参数 P 不是日期值。mysql> SELECT PERIOD_ADD(9801,2); +---------------------------------------------------------+ | PERIOD_ADD(9801,2) | +---------------------------------------------------------+ | 199803 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
PERIOD_DIFF(P1,P2)
返回周期 P1 和 P2 之间的月数。这些周期 P1 和 P2 应采用 YYMM 或 YYYYMM 格式。请注意,句点参数 P1 和 P2 不是日期值。mysql> SELECT PERIOD_DIFF(9802,199703); +---------------------------------------------------------+ | PERIOD_DIFF(9802,199703) | +---------------------------------------------------------+ | 11 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
QUARTER(date)
返回日期的一年中的季度,范围为 1 到 4。mysql> SELECT QUARTER('98-04-01'); +---------------------------------------------------------+ | QUARTER('98-04-01') | +---------------------------------------------------------+ | 2 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
-
SEC_TO_TIME(seconds)
返回秒参数,转换为小时、分钟和秒,作为 'HH:MM:SS' 或 HHMMSS 格式的值,具体取决于函数是在字符串还是数字上下文中使用。mysql> SELECT SEC_TO_TIME(2378); +---------------------------------------------------------+ | SEC_TO_TIME(2378) | +---------------------------------------------------------+ | 00:39:38 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
STR_TO_DATE(str,format)
这是 DATE_FORMAT() 函数的逆函数。它需要一个字符串 str 和一个格式字符串格式。如果格式字符串同时包含日期和时间部分,则 STR_TO_DATE() 函数将返回 DATETIME 值。否则,如果字符串仅包含日期或时间部分,则返回 DATE 或 TIME 值。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)
-
SUBDATE(date,INTERVAL expr unit) and SUBDATE(expr,days)
当使用第二个参数的 INTERVAL 形式调用时,SUBDATE() 是 DATE_SUB() 的同义词。有关 INTERVAL 单位参数的信息,请参阅 DATE_ADD() 的讨论。mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY); +---------------------------------------------------------+ | DATE_SUB('1998-01-02', INTERVAL 31 DAY) | +---------------------------------------------------------+ | 1997-12-02 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT SUBDATE('1998-01-02', INTERVAL 31 DAY); +---------------------------------------------------------+ | SUBDATE('1998-01-02', INTERVAL 31 DAY) | +---------------------------------------------------------+ | 1997-12-02 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
SUBTIME(expr1,expr2)
SUBTIME() 函数返回 expr1 。expr2 表示为与 expr1 格式相同的值。expr1 值是时间或日期时间表达式,而 expr2 值是时间表达式。mysql> SELECT SUBTIME('1997-12-31 23:59:59.999999', -> '1 1:1:1.000002'); +---------------------------------------------------------+ | SUBTIME('1997-12-31 23:59:59.999999'... | +---------------------------------------------------------+ | 1997-12-30 22:58:58.999997 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
SYSDATE()
将当前日期和时间作为 'YYYY-MM-DD HH:MM:SS' 或 YYYYMMDDHHMMSS 格式的值返回,具体取决于函数是在字符串还是数字上下文中使用。mysql> SELECT SYSDATE(); +---------------------------------------------------------+ | SYSDATE() | +---------------------------------------------------------+ | 2006-04-12 13:47:44 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TIME(expr)
提取时间或日期时间表达式的时间部分expr并将其作为字符串返回。mysql> SELECT TIME('2003-12-31 01:02:03'); +---------------------------------------------------------+ | TIME('2003-12-31 01:02:03') | +---------------------------------------------------------+ | 01:02:03 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TIMEDIFF(expr1,expr2)
TIMEDIFF() 函数返回 expr1 。expr2 表示为时间值。这些 expr1 和 expr2 值是时间或日期和时间表达式,但两者必须属于同一类型。mysql> SELECT TIMEDIFF('1997-12-31 23:59:59.000001', -> '1997-12-30 01:01:01.000002'); +---------------------------------------------------------+ | TIMEDIFF('1997-12-31 23:59:59.000001'..... | +---------------------------------------------------------+ | 46:58:57.999999 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
使用单个参数,此函数将日期或日期时间表达式 expr 作为日期时间值返回。使用两个参数,它将时间表达式 expr2 添加到日期或日期时间表达式expr1并将结果作为日期时间值返回。mysql> SELECT TIMESTAMP('2003-12-31'); +---------------------------------------------------------+ | TIMESTAMP('2003-12-31') | +---------------------------------------------------------+ | 2003-12-31 00:00:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TIMESTAMPADD(unit,interval,datetime_expr)
此函数将整数表达式间隔添加到日期或日期时间表达式datetime_expr. 间隔的单位由 unit 参数给出,它应该是以下值之一 -- FRAC_SECOND
- 秒,分钟
- 时、日
- 星期
- 月
- 季度或
- 年
可以使用所示的关键字之一或使用 SQL_TSI_ 前缀指定单位值。例如,DAY 和 SQL_TSI_DAY 都是合法的。mysql> SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02'); +---------------------------------------------------------+ | TIMESTAMPADD(MINUTE,1,'2003-01-02') | +---------------------------------------------------------+ | 2003-01-02 00:01:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
返回日期或日期时间表达式 datetime_expr1 和 datetime_expr2 之间的整数差。结果的单位由 unit 参数给出。单位的合法值与 TIMESTAMPADD() 函数描述中列出的值相同。mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01'); +---------------------------------------------------------+ | TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01') | +---------------------------------------------------------+ | 3 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TIME_FORMAT(time,format)
此函数与 DATE_FORMAT() 函数一样使用,但格式字符串可能仅包含小时、分钟和秒的格式说明符。如果时间值包含大于 23 的小时部分,则 %H和 %k小时格式说明符产生的值大于通常范围 0 到 23。其他小时格式说明符产生模 12 的小时值。mysql> SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l'); +---------------------------------------------------------+ | TIME_FORMAT('100:00:00', '%H %k %h %I %l') | +---------------------------------------------------------+ | 100 100 04 04 4 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TIME_TO_SEC(time)
返回转换为秒的时间参数。mysql> SELECT TIME_TO_SEC('22:23:00'); +---------------------------------------------------------+ | TIME_TO_SEC('22:23:00') | +---------------------------------------------------------+ | 80580 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
TO_DAYS(date)
给定一个日期,返回一个天数(自 0 年以来的天数)。mysql> SELECT TO_DAYS(950501); +---------------------------------------------------------+ | TO_DAYS(950501) | +---------------------------------------------------------+ | 728779 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
如果不带参数调用,此函数将 Unix 时间戳(自 '1970-01-01 00:00:00' UTC 以来的秒数)作为无符号整数返回。如果使用日期参数调用 UNIX_TIMESTAMP(),它会返回参数的值作为自 '1970-01-01 00:00:00' UTC 以来的秒数。date 可以是 DATE 字符串、DATETIME 字符串、TIMESTAMP 或格式为 YYMMDD 或 YYYYMMDD 的数字。mysql> SELECT UNIX_TIMESTAMP(); +---------------------------------------------------------+ | UNIX_TIMESTAMP() | +---------------------------------------------------------+ | 882226357 | +---------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT UNIX_TIMESTAMP('1997-10-04 22:23:00'); +---------------------------------------------------------+ | UNIX_TIMESTAMP('1997-10-04 22:23:00') | +---------------------------------------------------------+ | 875996580 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
UTC_DATE, UTC_DATE()
以 'YYYY-MM-DD' 或 YYYYMMDD 格式返回当前 UTC 日期,具体取决于函数是在字符串还是数字上下文中使用。mysql> SELECT UTC_DATE(), UTC_DATE() + 0; +---------------------------------------------------------+ | UTC_DATE(), UTC_DATE() + 0 | +---------------------------------------------------------+ | 2003-08-14, 20030814 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
UTC_TIME, UTC_TIME()
将当前 UTC 时间作为 'HH:MM:SS' 或 HHMMSS 格式的值返回,具体取决于函数是在字符串还是数字上下文中使用。mysql> SELECT UTC_TIME(), UTC_TIME() + 0; +---------------------------------------------------------+ | UTC_TIME(), UTC_TIME() + 0 | +---------------------------------------------------------+ | 18:07:53, 180753 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
UTC_TIMESTAMP, UTC_TIMESTAMP()
返回当前 UTC 日期和时间作为 'YYYY-MM-DD HH:MM:SS' 或 YYYYMMDDHHMMSS 格式的值,具体取决于函数是在字符串还是数字上下文中使用。mysql> SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0; +---------------------------------------------------------+ | UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0 | +---------------------------------------------------------+ | 2003-08-14 18:08:04, 20030814180804 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
WEEK(date[,mode])
此函数返回日期的周数。WEEK() 的双参数形式允许您指定一周是从星期日还是星期一开始,以及返回值是否应在 0 到 53 或 1 到 53 的范围内。如果省略 mode 参数,使用 default_week_format 系统变量的值模式 一周的第一天 范围 第 1 周是第一周。 0 星期日 0-53 今年有一个星期天 1 周一 0-53 今年有3天以上 2 星期日 1-53 今年有一个星期天 3 周一 1-53 今年有3天以上 4 星期日 0-53 今年有3天以上 5 周一 0-53 今年有个星期一 6 星期日 1-53 今年有3天以上 7 周一 1-53 今年有个星期一 mysql> SELECT WEEK('1998-02-20'); +---------------------------------------------------------+ | WEEK('1998-02-20') | +---------------------------------------------------------+ | 7 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
WEEKDAY(date)
返回日期的工作日索引(0 = 星期一,1 = 星期二,. 6 = 星期日)。mysql> SELECT WEEKDAY('1998-02-03 22:23:00'); +---------------------------------------------------------+ | WEEKDAY('1998-02-03 22:23:00') | +---------------------------------------------------------+ | 1 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
WEEKOFYEAR(date)
以 1 到 53 范围内的数字形式返回日期的日历周。WEEKOFYEAR() 是一个与 WEEK(date,3) 等效的兼容性函数。mysql> SELECT WEEKOFYEAR('1998-02-20'); +---------------------------------------------------------+ | WEEKOFYEAR('1998-02-20') | +---------------------------------------------------------+ | 8 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
YEAR(date)
返回日期的年份,范围为 1000 到 9999,或 0 表示 .zero。日期。mysql> SELECT YEAR('98-02-03'); +---------------------------------------------------------+ | YEAR('98-02-03') | +---------------------------------------------------------+ | 1998 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
-
YEARWEEK(date), YEARWEEK(date,mode)
返回日期的年份和星期。mode 参数的工作方式与 WEEK() 函数的 mode 参数完全相同。结果中的年份可能与一年中第一周和最后一周的日期参数中的年份不同。mysql> SELECT YEARWEEK('1987-01-01'); +---------------------------------------------------------+ | YEAR('98-02-03')YEARWEEK('1987-01-01') | +---------------------------------------------------------+ | 198653 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
注意− 周数不同于 WEEK() 函数为可选参数 0 或 1 返回 (0) 的值,因为 WEEK() 然后返回给定年份上下文中的周。