SQL - 日期函数

下表列出了通过SQL提供的所有重要的日期和时间相关功能.您的RDBMS支持各种其他功能.给定的列表基于MySQL RDBMS.

Sr.No.功能&描述
1ADDDATE()

添加日期

2ADDTIME()

添加时间

3CONVERT_TZ()

从一个时区转换为另一个时区

4CURDATE()

返回当前日期

5CURRENT_DATE(),CURRENT_DATE

同义词CURDATE()

6CURRENT_TIME(),CURRENT_TIME

同义词CURTIME()

7CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP

同义词NOW()

8CURTIME()

返回当前时间

9DATE_ADD()

添加两个日期

10DATE_FORMAT()

格式化日期

11DATE_SUB()

减去两个日期

12DATE()

提取日期或日期时间表达式的日期部分

13DATEDIFF()

减去两个日期

14DAY()

DAYOFMONTH的同义词()

15DAYNAME()

返回工作日的名称

16DAYOFMONTH()

返回月中的某一天(1-31)

17 DAYOFWEEK()

返回参数的工作日索引

18DAYOFYEAR()

返回一年中的某一天(1-366)

19EXTRACT

提取日期的一部分

20FROM_DAYS()

将日期编号转换为日期

21FROM_UNIXTIME()

将日期格式化为UNIX时间戳

22HOUR()

提取小时

23LAST_DAY

返回参数的月份的最后一天

24LOCALTIME(),LOCALTIME

NOW()的同义词

25LOCALTIMESTAMP,LOCALTIMESTAMP( )

NOW的同义词()

26MAKEDATE()

创建年份和年中的日期

27MAKETIME

MAKETIME()

28MICROSECOND()

返回参数的微秒

29MINUTE()

从参数返回分钟

30MONTH()

从通过之日起返回月份

31MONTHNAME()

返回月份名称

32NOW()

返回当前日期和时间

33PERIOD_ADD()

将期间添加到年 - 月

34PERIOD_DIFF()

返回句点之间的月数

35QUARTER()

从日期参数返回季度

36SEC_TO_TIME()

将秒转换为'HH:MM:SS'格式

37SECOND()

返回第二个(0-59)

38STR_TO_DATE()

将字符串转换为日期

39SUBDATE()

当使用三个参数调用时,DATE_SUB()的同义词

40SUBTIME()

减去时间

41SYSDATE()

返回函数执行的时间

42TIME_FORMAT()

格式为时间

43TIME_TO_SEC()

返回转换为秒的参数

44TIME()

提取传递的表达式的时间部分

45TIMEDIFF()

减去时间

46TIMESTAMP()

使用单个参数,此函数返回日期或日期时间表达式.使用两个参数,参数的总和

47TIMESTAMPADD()

向日期时间表达式添加间隔

48TIMESTAMPDIFF()

从日期时间表达式中减去间隔

49TO_DAYS()

返回转换为天数的日期参数

50UNIX_TIMESTAMP()

返回UNIX时间戳

51UTC_DATE()

返回当前的UTC日期

52UTC_TIME()

返回当前UTC时间

53UTC_TIMESTAMP()

返回当前的UTC日期和时间

54WEEK()

返回周数

55WEEKDAY()

返回工作日索引

56WEEKOFYEAR()

返回日期的日历周(1-53)

57年()

返回年份

58YEARWEEK()

返回年份和周

 

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)

 

CURRENT_DATE和CURRENT_DATE()

CURRENT_DATE和CURRENT_DATE()是CURDATE()的同义词

 

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)

 

CURRENT_TIME和CURRENT_TIME()

CURRENT_TIME和CURRENT_TIME()是CURTIME()的同义词.

 

CURRENT_TIMESTAMP和CURRENT_TIMESTAMP()

CURRENT_TIMESTAMP和CURRENT_TIMESTAMP()是NOW()的同义词.

 

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(日期,INTERVAL expr单位),DATE_SUB(日期,INTERVAL expr单位)

这些函数执行日期算术运算. 日期是指定开始日期的DATETIME或DATE值. expr 是一个表达式,用于指定要从开始日期添加或减去的区间值. expr是一个字符串;对于负间隔,它可以以' - '开头.

单位是一个关键字,表示应该解释表达式的单位.

INTERVAL 关键字和单位说明符不区分大小写.

下表显示了每个expr参数的预期形式单位价值.

unit Value预期的exprFormat
MICROSECONDMICROSECONDS
SECONDSECONDS
MINUTEMINUTES
HOURHOURS
DAYDAYS
WEEKWEEKS
MONTHMONTHS
QUARTERQUARTERS
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'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE'DAYS HOURS:MINUTES'
DAY_HOUR'DAYS HOURS'
YEAR_MONTH'YEARS-MONTHS'

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(日期,格式)

此命令按格式格式化日期值string.

格式字符串中可以使用以下说明符.表单前需要'%'字符在说明符字符.

Sr.No.Specifier&描述
1

%a

缩写的工作日名称(Sun..Sat)

2

%b

缩写的月份名称(Jan..Dec)

3

%c

月,数字(0..12)

4

%D

日英文后缀的月份(0th,1st,2nd,3rd ,.)

5

%d

日期月,数字(00..31)

6

%e

日期,数字ic(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

月份名称(1月〜12月) )

16

%m

月,数字(00 .. 12)

17

%p

AM或PM

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

%x

x,对于any.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)

DATE_SUB(date,INTERVAL expr unit)

This is similar to the DATE_ADD() function.

DAY(date)

The DAY() is a synonym for the DAYOFMONTH() function.

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)

Note  :  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)

LOCALTIME and LOCALTIME()

LOCALTIME and LOCALTIME() are synonyms for NOW().

LOCALTIMESTAMP and LOCALTIMESTAMP()

LOCALTIMESTAMP and LOCALTIMESTAMP() are synonyms for NOW().

MAKEDATE(year,dayofyear)

Returns a date, given year and day-of-year values. The dayofyear value must be greater than 0 or the result will be 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)

Returns a time value calculated from the hour, minute and second arguments.

 
 mysql> SELECT MAKETIME(12,15,30); 
 +---------------------------------------------------------+ 
 | MAKETIME(12,15,30)                                      | 
 +---------------------------------------------------------+ 
 | ’12:15:30’                                              | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

MICROSECOND(expr)

Returns the microseconds from the time or datetime expression (expr) as a number in the range from 0 to 999999.

 
 mysql> SELECT MICROSECOND(’12:00:00.123456’); 
 +---------------------------------------------------------+ 
 | MICROSECOND(’12:00:00.123456’)                          | 
 +---------------------------------------------------------+ 
 | 123456                                                  | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

MINUTE(time)

Returns the minute for time, in the range 0 to 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)

MONTH(date)

Returns the month for date, in the range 0 to 12.

 
 mysql> SELECT MONTH(’1998-02-03’) 
 +---------------------------------------------------------+ 
 | MONTH(’1998-02-03’)                                     | 
 +---------------------------------------------------------+ 
 | 2                                                       | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

MONTHNAME(date)

Returns the full name of the month for a date.

 
 mysql> SELECT MONTHNAME(’1998-02-05’); 
 +---------------------------------------------------------+ 
 | MONTHNAME(’1998-02-05’)                                 | 
 +---------------------------------------------------------+ 
 | February                                                | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

NOW()

Returns the current date and time as a value in ’YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context. This value is expressed in the current time zone.

 
 mysql> SELECT NOW(); 
 +---------------------------------------------------------+ 
 | NOW()                                                   | 
 +---------------------------------------------------------+ 
 | 1997-12-15 23:50:26                                     | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

PERIOD_ADD(P,N)

Adds N months to a period P (in the format YYMM or YYYYMM). Returns a value in the format YYYYMM. Note that the period argument P is not a date value.

 
 mysql> SELECT PERIOD_ADD(9801,2); 
 +---------------------------------------------------------+ 
 | PERIOD_ADD(9801,2)                                      | 
 +---------------------------------------------------------+ 
 | 199803                                                  | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

PERIOD_DIFF(P1,P2)

Returns the number of months between periods P1 and P2. These periods P1 and P2 should be in the format YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values.

 
 mysql> SELECT PERIOD_DIFF(9802,199703); 
 +---------------------------------------------------------+ 
 | PERIOD_DIFF(9802,199703)                                | 
 +---------------------------------------------------------+ 
 | 11                                                      | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

QUARTER(date)

Returns the quarter of the year for date, in the range 1 to 4.

 
 mysql> SELECT QUARTER(’98-04-01’); 
 +---------------------------------------------------------+ 
 | QUARTER(’98-04-01’)                                     | 
 +---------------------------------------------------------+ 
 | 2                                                       | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

SECOND(time)

Returns the second for time, in the range 0 to 59.

 
 mysql> SELECT SECOND(’10:05:03’); 
 +---------------------------------------------------------+ 
 | SECOND(’10:05:03’)                                      | 
 +---------------------------------------------------------+ 
 | 3                                                       | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

SEC_TO_TIME(seconds)

Returns the seconds argument, converted to hours, minutes and seconds, as a value in ’HH:MM:SS’ or HHMMSS format, depending on whether the function is used in a string or numeric context.

 
 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)

This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. The STR_TO_DATE() function returns a DATETIME value if the format string contains both date and time parts. Else, it returns a DATE or TIME value if the string contains only date or time parts.

 
 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)

When invoked with the INTERVAL form of the second argument, SUBDATE() is a synonym for DATE_SUB(). For information on the INTERVAL unit argument, see the discussion for 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)

The SUBTIME() function returns expr1 . expr2 expressed as a value in the same format as expr1. The expr1 value is a time or a datetime expression, while the expr2 value is a time expression.

 
 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()

Returns the current date and time 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.

 
 mysql> SELECT SYSDATE(); 
 +---------------------------------------------------------+ 
 | SYSDATE()                                               | 
 +---------------------------------------------------------+ 
 | 2006-04-12 13:47:44                                     | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

TIME(expr)

Extracts the time part of the time or datetime expression expr and returns it as a string.

 
 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)

The TIMEDIFF() function returns expr1 . expr2 expressed as a time value. These expr1 and expr2 values are time or date-and-time expressions, but both must be of the same type.

 
 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)

With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.

 
 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)

This function adds the integer expression interval to the date or datetime expression datetime_expr. The unit for interval is given by the unit argument, which should be one of the following values  :

  • FRAC_SECOND

  • SECOND, MINUTE

  • HOUR, DAY

  • WEEK

  • MONTH

  • QUARTER or

  • YEAR

The unit value may be specified using one of the keywords as shown or with a prefix of SQL_TSI_.

For example, DAY and SQL_TSI_DAY both are legal.

 
 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)

Returns the integer difference between the date or datetime expressions datetime_expr1 and datetime_expr2. The unit for the result is given by the unit argument. The legal values for the unit are the same as those listed in the description of the TIMESTAMPADD() function.

 
 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)

This function is used like the DATE_FORMAT() function, but the format string may contain format specifiers only for hours, minutes and seconds.

If the time value contains an hour part that is greater than 23, the %H and %k hour format specifiers produce a value larger than the usual range of 0 to 23. The other hour format specifiers produce the hour value modulo 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)

Returns the time argument converted to seconds.

 
 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)

Given a date, returns a day number (the number of days since year 0).

 
 mysql> SELECT TO_DAYS(950501); 
 +---------------------------------------------------------+ 
 | TO_DAYS(950501)                                         | 
 +---------------------------------------------------------+ 
 | 728779                                                  | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)

If called with no argument, this function returns a Unix timestamp (seconds since ’1970-01-01 00:00:00’ UTC) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since ’1970-01-01 00:00:00’ UTC. date may be a DATE string, a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or 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()

Returns the current UTC date as a value in ’YYYY-MM-DD’ or YYYYMMDD format, depending on whether the function is used in a string or numeric context.

 
 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()

Returns the current UTC time as a value in ’HH:MM:SS’ or HHMMSS format, depending on whether the function is used in a string or numeric context.

 
 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()

Returns the current UTC date and time as a value in ’YYYY-MM-DD HH:MM:SS’ or in a YYYYMMDDHHMMSS format, depending on whether the function is used in a string or in a numeric context.

 
 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])

This function returns the week number for date. The two-argument form of WEEK() allows you to specify whether the week starts on a Sunday or a Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If the mode argument is omitted, the value of the default_week_format system variable is used

$b$ b

ModeFirst Day of weekRangeWeek 1 is the first week.
0Sunday0-53with a Sunday in this year
1Monday0-53with more than 3 days this year
2Sunday1-53with a Sunday in this year
3Monday1-53with more than 3 days this year
4Sunday0-53with more than 3 days this year
5Monday0-53with a Monday in this year
6Sunday1-53with more than 3 days this year
7Monday1-53with a Monday in this year
 
 mysql> SELECT WEEK(’1998-02-20’); 
 +---------------------------------------------------------+ 
 | WEEK(’1998-02-20’)                                      | 
 +---------------------------------------------------------+ 
 | 7                                                       | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

WEEKDAY(date)

Returns the weekday index for date (0 = Monday, 1 = Tuesday, . 6 = Sunday).

 
 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)

Returns the calendar week of the date as a number in the range from 1 to 53. WEEKOFYEAR() is a compatibility function that is equivalent to WEEK(date,3).

 
 mysql> SELECT WEEKOFYEAR(’1998-02-20’); 
 +---------------------------------------------------------+ 
 | WEEKOFYEAR(’1998-02-20’)                                | 
 +---------------------------------------------------------+ 
 | 8                                                       | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

YEAR(date)

Returns the year for date, in the range 1000 to 9999, or 0 for the .zero. date.

 
 mysql> SELECT YEAR(’98-02-03’); 
 +---------------------------------------------------------+ 
 | YEAR(’98-02-03’)                                        | 
 +---------------------------------------------------------+ 
 | 1998                                                    | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

YEARWEEK(date), YEARWEEK(date,mode)

Returns the year and the week for a date. The mode argument works exactly like the mode argument to the WEEK() function. The year in the result may be different from the year in the date argument for the first and the last week of the year.

 
 mysql> SELECT YEARWEEK(’1987-01-01’); 
 +---------------------------------------------------------+ 
 | YEAR(’98-02-03’)YEARWEEK(’1987-01-01’)                  | 
 +---------------------------------------------------------+ 
 | 198653                                                  | 
 +---------------------------------------------------------+ 
 1 row in set (0.00 sec)

Note  :  The week number is different from what the WEEK() function would return (0) for optional arguments 0 or 1, as WEEK() then returns the week in the context of the given year.