Changing datatype of calculation in PostgreSQL

Viewed 21

I am extracting the year from a date and subtracting it from another year but for some reason I keep getting a float as a result. How can I fix this? Thanks a bunch

    SELECT 2022-(EXTRACT(YEAR FROM CAST(birthdate AS DATE)))AS age
    FROM animals

Result:Result

1 Answers

Because EXTRACT function return type was double precision, if you want to get value of integer you can try to convert the type to INT after using EXTRACT.

SELECT 2022-(EXTRACT(YEAR FROM CAST(birthdate AS DATE)))::INT AS age
FROM animals
Related