AGE
MySQL equivalent:
Returns the difference between two timestamps in years, months and days.
PostgreSQL 17.9
AGE([timestamp1, ]timestamp2)- [ ]optional part
- timestamp1
- Later timestamp, the current date (CURRENT_DATE) by defaulttimestampOptional
- timestamp2
- Earlier timestamp that is subtractedtimestamp
Return value
An interval in years, months and days; negative if timestamp1 is earlier than timestamp2
Examples
Difference between two dates
PostgreSQL 17.9
SELECT AGE('2022-12-05', '2012-06-12');Age of family members today
PostgreSQL 17.9
SELECT member_name,
AGE(birthday) AS age
FROM FamilyMembers;Details
With one argument the age is counted from midnight of the current date. Full years are taken with EXTRACT(YEAR FROM AGE(birthday)).
Plain subtraction timestamp - timestamp also gives an interval, but in days and hours, without months and years: 67 days 10:00:00 instead of 2 mons 7 days 10:00:00.