Skip to content
Advertisement

Oracle query to calculate current age

I want to calculate current age of person from DOB(date of birth) field in Oracle table.

Data type of DOB field is varchar and the is date stored in format ‘DD-MON-YY’.

when I calculate current age of a person from date like 10-JAN-49 the query will return age in negative. Also, I observed that if date has year 13 to 49 it gives negative result.

Examples

Query Executed for reference

Any help is appreciated!

Advertisement

Answer

To get round the 21st century problem, just modifying @the_silk’s answer slightly:

Please be aware though that this assumes that any date year between ’00’ and ’13’ is 21st century, so this sql should only be used if you are building a one off throwaway script, otherwise it will become out of date and invalid before long.

The best solution would be to rebuild this table, converting the varchar column into a date column, as alluded to by Ben.

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