Skip to content
Advertisement

Calculate age from date of birth with formatting

I would like to calculate the age from the date of birth stored as a string using the format in the comments in the code. Here’s my code:

The result I get is the following:

I guess there’s something wrong with the year but I can’t figure it out!

Advertisement

Answer

The error is in this line:

You should not call to_date on sysdate, because sysdate already has the data type date.

You should just do:

Or, even leave out that variable, and use sysdate directly in your calculation.

I would also change the other date format to YYYYMMDD, since your table data has no hyphens.

Note that you can do this without PL/SQL, with a query:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement