--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天的缺陷。