Name

DATE_SUB()

Synopsis

DATE_SUB(date, INTERVAL number type)

Use this function to subtract from the results of a date or time data type column. See Table 12-1, under the description of DATE_ADD(), for a list of interval types. Here is an example of this function:

SELECT NOW( ) AS Today,
DATE_SUB(NOW( ), INTERVAL 1 DAY)
AS Yesterday;

+---------------------+---------------------+
| Today               | Yesterday           |
+---------------------+---------------------+
| 2007-05-14 14:26:54 | 2007-05-13 14:26:54 | 
+---------------------+---------------------+

Notice in this example that the time remains unchanged, but the date was reduced by one day. If you place a negative sign in front of the value 1, the reverse effect will occur, giving a result of May 15 in this example. Any intervals that can be used with DATE_ADD() can also be used with DATE_SUB().

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.