In PostgreSQL, we can use the +
operator to add one or more years to a date.
Examples
We can specify intervals when adding to dates. For example, we can use year
or years
to add one or more years:
SELECT date '2030-01-20' + interval '1 year';
Result:
2031-01-20 00:00:00
And in plural form:
SELECT date '2030-01-20' + interval '2 years';
Result:
2032-01-20 00:00:00
Specified in Months
We can also use the equivalent number in months (or weeks or days for that matter):
SELECT date '2030-01-20' + interval '12 months';
Result:
2031-01-20 00:00:00
We can also use an integer when specifying it in days:
SELECT date '2030-01-20' + 365;
Result:
2031-01-20
Or like this:
SELECT date '2030-01-20' + integer '365';
Result:
2031-01-20
Negative Values
It’s possible to perform date arithmetic with negative values. If we use a negative value with the +
sign, then the specified number of years 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 years';
Result:
2032-01-20 00:00:00
We can do the same thing with the integer option:
SELECT date '2030-01-20' - integer '-365';
Result:
2031-01-20