Date and Time Functions
919
•
DATEDIFF(expr1,expr2)
[919]
DATEDIFF()
[919]
returns
expr1
–
expr2
expressed as a value in days from one date to the
other.
expr1
and
expr2
are date or date-and-time expressions. Only the date parts of the values
are used in the calculation.
mysql>
SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
-> 1
mysql>
SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
-> -31
•
DATE_ADD(date,INTERVAL expr unit)
[919]
,
DATE_SUB(date,INTERVAL expr
unit)
[922]
These functions perform date arithmetic. The
date
argument specifies the starting date or datetime
value.
expr
is an expression specifying the interval value to be added or subtracted from the starting
date.
expr
is a string; it may start with a “
-
” for negative intervals.
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
expr
Format
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:SECONDS.MICROSECONDS'
MINUTE_SECOND
'MINUTES:SECONDS'
HOUR_MICROSECOND
'HOURS:MINUTES:SECONDS.MICROSECONDS'
HOUR_SECOND
'HOURS:MINUTES:SECONDS'
HOUR_MINUTE
'HOURS:MINUTES'
DAY_MICROSECOND
'DAYS
HOURS:MINUTES:SECONDS.MICROSECONDS'
DAY_SECOND
'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE
'DAYS HOURS:MINUTES'
DAY_HOUR
'DAYS HOURS'
YEAR_MONTH
'YEARS-MONTHS'
The values
QUARTER
and
WEEK
are available beginning with MySQL 5.0.0.
The return value depends on the arguments:
•
DATETIME
if the first argument is a
DATETIME
(or
TIMESTAMP
) value, or if the first argument is a
DATE
and the
unit
value uses
HOURS
,
MINUTES
, or
SECONDS
.
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...