In SQLite, we can use the DATE()
function to subtract one or more months from a date.
For datetime values, we can use the DATETIME()
function.
Example
Here’s an example that uses the DATE()
function:
SELECT DATE('2050-08-21', '-1 month');
Result:
2050-07-21
If we wanted to add the amount, we could replace -
with +
, or omit it altogether.
We can specify the months in plural or non-plural form. In other words, month
is equivalent to months
:
SELECT
DATE('2050-08-21', '-2 month') AS month,
DATE('2050-08-21', '-2 months') AS months;
Result:
month months ---------- ---------- 2050-06-21 2050-06-21
Specified in Days
We can also subtract months based on a number of days:
SELECT DATE('2050-08-21', '-31 days');
Result:
2050-07-21
The DATETIME()
Function
This example uses the DATETIME()
function to subtract a month from a datetime value:
SELECT DATETIME('2050-08-21 18:30:45', '-1 month');
Result:
2050-07-21 18:30:45