w3resource

PostgreSQL AGE() function

AGE() function

The PostgreSQL AGE() function subtracts two timestamps, producing a symbolic result that uses years and months.

Uses of AGE() Function
  • Calculate Age: Determine the age in years, months, and days between two dates.

  • Date Difference: Find the difference between a specific date and the current date.

  • Time Span Analysis: Useful for age-related calculations in demographics, employee tenure, etc.

  • Historical Data Comparison: Compare dates in historical data for reporting and analysis.

Syntax:

age(timestamp, timestamp)
or
age(timestamp)

Return Type: interval

PostgreSQL Version: 9.3

Pictorial Presentation of PostgreSQL AGE() function

Pictorial presentation of postgresql age function

Example 1: PostgreSQL AGE() function

The example below finds the age between two dates specified in the argument.

Code

SELECT age(timestamp '2015-01-15', timestamp '1972-12-28');

Sample Output:

       age
------------------
 42 years 18 days
(1 row)

Example 2:

The example below finds the age between current date and the date as specified in the argument.

Code

SELECT age(timestamp '2007-10-07');

Sample Output:

          age
-----------------------
 7 years 3 mons 7 days
(1 row)

Previous: Date and Time Operators
Next: CLOCK_TIMESTAMP function



Follow us on Facebook and Twitter for latest update.