PostgreSQL Age Calculation from date type

前端 未结 2 772
执念已碎
执念已碎 2020-12-18 04:07

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,
C         


        
相关标签:
2条回答
  • 2020-12-18 04:53

    Use date_part() and age() functions

    SELECT name text, date_part('year',age(date_of_birth)),* FROM public.employees
    

    See for documentation of date functions https://www.postgresql.org/docs/current/static/functions-datetime.html

    0 讨论(0)
  • 2020-12-18 05:04

    you can use age() function like this :

    SELECT name, EXTRACT(year FROM age(current_date,date_of_birth)) :: int as age FROM public.employees
    
    0 讨论(0)
提交回复
热议问题