Name

SUBDATE()

Synopsis

SUBDATE(date, INTERVAL value type)
SUBDATE(date days)

Use this function to subtract a date or time interval from the results of a DATE or TIME data type column. It’s an alias for DATE_SUB(). If a negative value is given, the interval specified is added instead of subtracted. This is the equivalent of ADDDATE(). See Table 12-1 under DATE_ADD() earlier in this chapter for a list of intervals permitted. Here is an example:

SELECT SUBDATE(NOW( ), 1) AS 'Yesterday',
SUBDATE(NOW( ), INTERVAL -1 DAY) AS 'Tomorrow';

+---------------------+---------------------+
| Yesterday           | Tomorrow            |
+---------------------+---------------------+
| 2008-05-09 16:11:56 | 2008-05-11 16:11:56 |
+---------------------+---------------------+

As of version 4.1 of MySQL, when subtracting days you can just give the number of days for the second argument (i.e., just 1 instead of INTERVAL 1 DAY).

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.