Postgresql 通过身份证获取周岁年龄(精确到周岁)

--sfzh为18位的身份证号码,从第七位起,取8位数字,如19980101
select cast (data_part('year', age(to_date(substr(sfzh, 7, 8), 'yyyymmdd'))) as integer)

--
PostgreSQL age () function is used to calculate the age between two dates, it will return the number of years, days, and months between the two different dates. Age function in PostgreSQL will accept the two arguments as date timestamp and return the calculated difference between two different dates.

--
The DATE_PART () function returns a double precision type value.
DATE_PART() function is one of the date-time functions available in PostgreSQL that allows you to retrieve subfields like., year, month, week, etc., from a date or time value.

--
the CAST function is used to convert the data type of a value to a different data type. The value must be compatible with the targeted data type; otherwise, an error will be returned

解决单纯用日期  - 365天的缺陷。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值