How to get year year () function only in postgresql select query

select age(cast(dob as date)) 
from mas_patient_details 

when I run over the request, it returns

age
----------
39 years 5 mons 19 days
13 years 2 days
69 years 2 days
41 years 11 mons 25 days

select age (cast (dob as date)) from mas_patient_details, where age <= 59

he returns

39 years 5 mons 19 days
13 years 2 days
69 years 2 days
41 years 11 mons 25 days

how is my request right?

+4
source share
1 answer

If the fu column has a DATE data type, you can use

SELECT EXTRACT(YEAR FROM fu) FROM mydate;

and if it is varchar, you convert it for today using to_date ()

SELECT EXTRACT(YEAR FROM to_date(fu, <your pattern>)) FROM mydate;

In your case:

select EXTRACT(YEAR FROM age(cast(dob as date))) 
from mas_patient_details 
where age <= 59;
+6
source

Source: https://habr.com/ru/post/1660170/


All Articles