Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL Age Calculation from date type

Tags:

postgresql

I have the following table:

CREATE TABLE public.employees
(
employee_id integer NOT NULL,
name text NOT NULL,
date_of_birth date,
address text,
email text,
CONSTRAINT employees_pkey PRIMARY KEY (employees_id),
CONSTRAINT employees_email_key UNIQUE (email)
)

How would I be able to list each employee's name with their age in the output?

Thanks.

like image 262
Daniel D Avatar asked Oct 16 '16 16:10

Daniel D


Video Answer


1 Answers

you can use age() function like this :

SELECT name, EXTRACT(year FROM age(current_date,date_of_birth)) :: int as age FROM public.employees
like image 101
Meyyappan Avatar answered Oct 05 '22 04:10

Meyyappan