In SQLite, we can use the DATE()
function to subtract one or more days 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 day');
Result:
2050-08-20
If we wanted to add the amount, we could replace -
with +
, or omit it altogether.
We can specify the days in plural or non-plural form. In other words, day
is equivalent to days
:
SELECT
DATE('2050-08-21', '-2 day') AS day,
DATE('2050-08-21', '-2 days') AS days;
Result:
day days ---------- ---------- 2050-08-19 2050-08-19
Specified in Hours
We can also subtract days based on a number of hours:
SELECT DATE('2050-08-21', '-24 hour');
Result:
2050-08-20
The DATETIME()
Function
This example uses the DATETIME()
function to subtract a day from a datetime value:
SELECT DATETIME('2050-08-21 18:30:45', '-1 day');
Result:
2050-08-20 18:30:45