In PostgreSQL, we can use the -
operator to subtract one or more years from a date.
Examples
We can specify intervals when subtracting from dates. For example, we can use year
or years
to subtract one or more years from a date:
SELECT date '2030-01-20' - interval '1 year';
Result:
2029-01-20 00:00:00
And in plural form:
SELECT date '2030-01-20' - interval '5 years';
Result:
2025-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:
2029-01-20 00:00:00
We can also use an integer when specifying it in days:
SELECT date '2030-01-20' - 365;
Result:
2029-01-20
Or like this:
SELECT date '2030-01-20' - integer '365';
Result:
2029-01-20
Adding Negative Values
We can alternatively add a negative value to the date. In this case, we can use a negative value with the +
sign:
Example:
SELECT date '2030-01-20' + interval '-7 years';
Result:
2023-01-20 00:00:00
We can do the same thing with the integer option:
SELECT date '2030-01-20' + integer '-365';
Result:
2029-01-20