We have a birthday column in the format DD.MM.YYYY (for example 25.04.1990) of string type. How can we find out the employee’s age at the time of the query?
I do not know how to change to date type and how to find the age at the time of the request, without specifying today’s date
2
Answers
You need to convert that string to a proper date, then you can e.g. use the
age()
function:ALTER
your "string type" column to the appropriate typedate
first:Postgres uses the system time of the underlying OS. Compute an "age" with the
age()
function, either providing an end-timestamp explicitly with the 2-parameter form, or defaulting to the system time implicitly with the 1-parameter form:The function takes
timestamp
. Butdate
(ortimestamptz
) are coerced automatically.Use
EXTRACT()
(orto_char()
) to get to the age as customaryinteger
(ortext
):