Jump to: navigation, search

PostgreSQL AGE

From w3cyberlearnings

Contents

Postgresql age

This function returns the total year, month, and day for a date.

Syntax age

  • two arguments: calculate the age for the date2 based on the date1
  • single argument: calculate the age from the date based on the current date at midnight.
age(timestamp date1, timestamp date2);

OR

age(date);

Example 1

postgres=# SELECT age(timestamp '1985-03-15');
       age       
-----------------
 27 years 3 mons
(1 row)

Example 2

postgres=# SELECT age(timestamp '2012-06-15', timestamp '1985-03-15');
       age       
-----------------
 27 years 3 mons
(1 row)

postgres=# SELECT age(timestamp '2010-06-15', timestamp '1985-03-15');
       age       
-----------------
 25 years 3 mons
(1 row)

Example 3

postgres=# SELECT name, dob, age(dob) age1,age(timestamp '2010-1-1',dob) age2
FROM employee;
  name  |    dob     |          age1          |           age2           
--------+------------+------------------------+-------------------------
 John   | 1990-04-13 | 22 years 2 mons 3 days | 19 years 8 mons 18 days
 Week   | 1991-06-12 | 21 years 4 days        | 18 years 6 mons 19 days
 Christ | 1995-05-17 | 17 years 30 days       | 14 years 7 mons 15 days
(3 rows)

Related Links



Navigation
Web
SQL
MISC
References