In PostgreSQL, we can use the +
operator to add one or more weeks to a date. We have a few options when it comes to specifying the actual number of weeks.
Examples
The following examples demonstrate various options for specifying the number of weeks to add to the date.
Postgres allows us to specify intervals when adding to dates. We can therefore specify week
or weeks
:
SELECT date '2030-01-20' + interval '1 week';
Result:
2030-01-27 00:00:00
This also works in plural form:
SELECT date '2030-01-20' + interval '2 weeks';
Result:
2030-02-03 00:00:00
Specified in Days
We can also use the equivalent number in days.
SELECT date '2030-01-20' + interval '7 days';
Result:
2030-01-27 00:00:00
When specifying it in days, we can use an integer:
SELECT date '2030-01-20' + 7;
Result:
2030-01-27
We can also do it like this:
SELECT date '2030-01-20' + integer '7';
Result:
2030-01-27
Negative Values
We can also perform date arithmetic with negative values. If we use a negative value with the +
sign, then the specified number of weeks will be subtracted from the date. But if we use it with the -
sign, then it will be added to the date.
Example:
SELECT date '2030-01-20' - interval '-2 weeks';
Result:
2030-02-03 00:00:00
We can do the same thing with the integer option:
SELECT date '2030-01-20' - integer '-14';
Result:
2030-02-03