Understanding the AGE Function in PostgreSQL

Updated: January 5, 2024 By: Guest Contributor Post a comment

Overview

PostgreSQL, often simply Postgres, is a powerful, open-source object-relational database system that uses and extends the SQL language. It provides numerous functions for developers and database administrators to work efficiently with dates and times. One such function is the AGE function, which offers a convenient way to calculate intervals between timestamps.

Background

The AGE function was introduced in PostgreSQL as part of its versatile set of date/time functions. It has been a feature of PostgreSQL for many versions, its purpose is invaluable in calculating age or duration between two timestamps.

Use Cases

The primary purpose of the AGE function is to calculate the difference between two points in time and return the result as an interval. It can be beneficial when you need to determine how much time has elapsed between two dates or simply calculate someone or something’s age given a birthdate and the current date.

Syntax and Parameters

Syntax:

AGE(timestamp1, timestamp2)

Parameters:

  • timestamp1: The later point in time. If not specified, defaults to the current date and time.
  • timestamp2: The earlier point in time. Returned Value: An interval showing the difference between timestamp1 and timestamp2.

Practical Examples

Example 1: Basic Age Calculation

Calculate the age of a person given their birthdate.

Using AGE to figure out the exact age from a birthdate.

SELECT AGE(TIMESTAMP '2000-01-01'); -- Assumes current date as the first parameter

Example 2: Duration Between Dates

Calculate the duration between two specific dates.

This example shows how to calculate the duration of a past event from today:

SELECT AGE(NOW(), TIMESTAMP '1969-07-20 20:18:04+00'); -- The exact moment of the first moon landing

Example 3: Age at Event

Determine how old a person was on a certain event date.

Often useful for historical records or to establish context, here we calculate someone’s age on a significant past date:

SELECT AGE(TIMESTAMP '2001-09-11', TIMESTAMP '2000-01-01');

Example 4: Interval Formatting

Format interval output for clarity.

Sometimes it is more useful to display the age result in years, months and days instead of the standard interval format:

SELECT EXTRACT(YEAR FROM AGE(TIMESTAMP '2000-01-01')) AS years,
       EXTRACT(MONTH FROM AGE(TIMESTAMP '2000-01-01')) AS months,
       EXTRACT(DAY FROM AGE(TIMESTAMP '2000-01-01')) AS days;

Conclusion

The AGE function in PostgreSQL is an essential tool when working with dates and times. It simplifies the process of interval calculation between timestamps, which is indispensable in many real-world applications like calculating ages, durations, and periods of time. Understanding and utilizing the AGE function can help keep your queries concise and efficient. With the provided examples, you should have a solid foundation for incorporating this function into your PostgreSQL toolbox.