DATE_SUB( )

Use this MySQL function to subtract from the results of a date or time data type column. It's fairly synonymous with the SUBDATE( ).

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

DATE_SUB(date, INTERVAL number type)

Explanation

Use this function to subtract from the results of a date or time data type column. See the description of DATE_ADD( ), for a list of interval types.

Examples

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. By placing a negative sign in front of the value the reverse effect would 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( ).