PostgreSQL从日期类型计算年龄 [英] PostgreSQL Age Calculation from date type

查看:2098
本文介绍了PostgreSQL从日期类型计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

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?

谢谢。

推荐答案

使用date_part( )和age()函数

Use date_part() and age() functions

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

有关日期函数的文档,请参见 https://www.postgresql.org/docs/current/static/functions-datetime.html

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

这篇关于PostgreSQL从日期类型计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆