How to round age with Postgresql?

Asked

Viewed 1,487 times

2

I used the function age(), below, to calculate age, from a date stored in a table:

select pessoa.*, age(data_nascimento) from pessoa;

Returned the range: 27 years 9 months 9 days

It is possible to round this range only to 27 years or 27 years years?

The DBMS used: Postgresql.

2 answers

2


I like to use the to_char, brings many possibilities, follows example of various functions:

select *,
   date_part('year', age(data_nascimento))||' Anos' AS idade,
   date_trunc('year', age(data_nascimento)) AS idade2,
   extract(year from age(data_nascimento))||' Anos' AS idade3,
   to_char(age(data_nascimento),'yy Anos') as idade4
from pessoa

More information from to_char http://www.postgresql.org/docs/9.4/static/functions-formatting.html

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.