In MariaDB, DATE_SUB()
is a built-in date and time function that allows you to subtract an amount from a date.
It allows you to change a date by specifying the date, the unit to subtract, and the amount to subtract. You can pass a negative amount in order to add to the date, instead of subtracting from it.
Syntax
The syntax goes like this:
DATE_SUB(date,INTERVAL expr unit)
Where date
is the date to change, expr
is the amount to subtract, and unit
is the date/time unit to subtract (e.g. second, minute, etc).
Example
Here’s a basic example:
SELECT DATE_SUB('2021-05-31 10:00:00', INTERVAL 1 HOUR);
Result:
+--------------------------------------------------+ | DATE_SUB('2021-05-31 10:00:00', INTERVAL 1 HOUR) | +--------------------------------------------------+ | 2021-05-31 09:00:00 | +--------------------------------------------------+
This is like doing the following:
SELECT '2021-05-31 10:00:00' - INTERVAL 1 HOUR;
Result:
+-----------------------------------------+ | '2021-05-31 10:00:00' - INTERVAL 1 HOUR | +-----------------------------------------+ | 2021-05-31 09:00:00 | +-----------------------------------------+
Negative Intervals
Providing a negative interval adds that amount from the date.
Example:
SELECT DATE_SUB('2021-05-31 10:00:00', INTERVAL -1 HOUR);
Result:
+---------------------------------------------------+ | DATE_SUB('2021-05-31 10:00:00', INTERVAL -1 HOUR) | +---------------------------------------------------+ | 2021-05-31 11:00:00 | +---------------------------------------------------+
Other Units
Here’s an example that adds an interval of 1 to the various date and time units:
SELECT
DATE_SUB('2021-05-10 10:00:00', INTERVAL 1 YEAR) AS YEAR,
DATE_SUB('2021-05-10 10:00:00', INTERVAL 1 MONTH) AS MONTH,
DATE_SUB('2021-05-10 10:00:00', INTERVAL 1 DAY) AS DAY,
DATE_SUB('2021-05-10 10:00:00', INTERVAL 1 HOUR) AS HOUR,
DATE_SUB('2021-05-10 10:00:00', INTERVAL 1 MINUTE) AS MINUTE,
DATE_SUB('2021-05-10 10:00:00', INTERVAL 1 SECOND) AS SECOND,
DATE_SUB('2021-05-10 10:00:00', INTERVAL 1 MICROSECOND) AS MICROSECOND;
Result (using vertical output):
YEAR: 2020-05-10 10:00:00 MONTH: 2021-04-10 10:00:00 DAY: 2021-05-09 10:00:00 HOUR: 2021-05-10 09:00:00 MINUTE: 2021-05-10 09:59:00 SECOND: 2021-05-10 09:59:59 MICROSECOND: 2021-05-10 09:59:59.999999
Composite Units
Here’s an example that uses composite units:
SELECT
DATE_SUB('2021-05-01 10:00:00', INTERVAL '1:2' YEAR_MONTH) AS "YEAR_MONTH",
DATE_SUB('2021-05-01 10:00:00', INTERVAL '1:25:35' HOUR_SECOND) AS "HOUR_SECOND",
DATE_SUB('2021-05-01 10:00:00', INTERVAL '1:30' DAY_MINUTE) AS "DAY_MINUTE";
Result:
+---------------------+---------------------+---------------------+ | YEAR_MONTH | HOUR_SECOND | DAY_MINUTE | +---------------------+---------------------+---------------------+ | 2020-03-01 10:00:00 | 2021-05-01 08:34:25 | 2021-05-01 08:30:00 | +---------------------+---------------------+---------------------+
Null Dates
Passing null
for the date returns null
:
SELECT DATE_SUB(null, INTERVAL 1 YEAR);
Result:
+---------------------------------+ | DATE_SUB(null, INTERVAL 1 YEAR) | +---------------------------------+ | NULL | +---------------------------------+
Missing Argument
Calling DATE_SUB()
with the wrong number of arguments, or without passing any arguments results in an error:
SELECT DATE_SUB();
Result:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1