Subtract a Month from a Date in MariaDB

MariaDB provides several ways to perform arithmetic on dates. This includes adding or subtracting a month (or many months) from a given date.

Here’s an example of subtracting a month from a date in MariaDB.

The DATE_SUB() Function

The DATE_SUB() function allows you to subtract a certain number of units from a date. Therefore you can return the date, minus a number of days, weeks, months, years, etc.

In our case, we want to subtract a month.

Example:

SELECT DATE_SUB('2021-05-01', INTERVAL 1 MONTH);

Result:

+------------------------------------------+
| DATE_SUB('2021-05-01', INTERVAL 1 MONTH) |
+------------------------------------------+
| 2021-04-01                               |
+------------------------------------------+

Multiple Months

If you need to subtract more than one month, just use that number instead of 1. However, the MONTH keyword remains non-plural regardless:

SELECT DATE_SUB('2021-05-01', INTERVAL 2 MONTH);

Result:

+------------------------------------------+
| DATE_SUB('2021-05-01', INTERVAL 2 MONTH) |
+------------------------------------------+
| 2021-03-01                               |
+------------------------------------------+

The Subtraction Operator (-)

Another way to subtract a month from a date is to use the subtraction operator (-), also known as the minus operator.

Example:

SELECT '2021-05-01' - INTERVAL 1 MONTH;

Result:

+---------------------------------+
| '2021-05-01' - INTERVAL 1 MONTH |
+---------------------------------+
| 2021-04-01                      |
+---------------------------------+

The Addition Operator (+)

And if you prefer to be convoluted, you could use the addition operator (+) along with a negative amount.

Example:

SELECT '2021-05-01' + INTERVAL -1 MONTH;

Result:

+----------------------------------+
| '2021-05-01' + INTERVAL -1 MONTH |
+----------------------------------+
| 2021-04-01                       |
+----------------------------------+

Other Ways to Subtract Months from Dates

Here are some other approaches you could use to subtract one or more months from a date:

  • The SUBDATE() function (this is a synonym for DATE_SUB() when used with the same syntax).
  • The DATE_ADD() function (providing a negative amount will subtract that amount from the date).
  • The ADDDATE() function (providing a negative amount will subtract that amount from the date).