We can use the -
operator to subtract one or more weeks from a date in PostgreSQL.
Examples
We can specify intervals when subtracting from dates. To subtract weeks, we can use week
or weeks
:
SELECT date '2040-01-17' - interval '1 week';
Result:
2040-01-10 00:00:00
And in plural form:
SELECT date '2040-01-17' - interval '5 weeks';
Result:
2039-12-13 00:00:00
Specified in Days
Another way to do it is to specify the number in its daily equivalent:
SELECT date '2040-01-17' - interval '7 days';
Result:
2040-01-10 00:00:00
We can also use an integer when specifying it in days:
SELECT date '2040-01-17' - 7;
Result:
2040-01-10
Or like this:
SELECT date '2040-01-17' - integer '7';
Result:
2040-01-10
Adding Negative Values
We can also add negative values to dates. To subtract a negative amount, we can use the negative value with the +
sign:
Example:
SELECT date '2040-01-17' + interval '-5 weeks';
Result:
2039-12-13 00:00:00
We can do the same thing with the integer option:
SELECT date '2040-01-17' + integer '-35';
Result:
2039-12-13