Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: SQL Oracle
I want to calculate current age of person from dob field in Oracle table.
Data type of DOB field is varchar and date stored in format 'DD-MON-YY'.
when I calculate current age of from date like 10-JAN-49 then query result age in negative.
and I observed that if date has year 13 to 49 it gives negative result.
22-NOV-83 -valid result
09-FEB-58 --valid result
05-JUN-49 - Invalid result like -36
Query Executed for reference
select round(MONTHS_BETWEEN(sysdate,to_date(dob,'DD-MON-RR'))/12)||' Yrs' from birth
Please help me.
Posted 2-Jul-13 0:45am
Sudhakar Shinde at 2-Jul-13 7:20am
I tried below query which works well..
That means something is wrong with the data in your birth table. Please check the data for dob column is correct. If possible you can share it in your question.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That's caused by your defective design! How often can you read on CP and other programming sites that a date MUST NOT be stored as a char/varchar/nvarchar?
Looks like 05-JUN-49 gets interpreted as 05-JUN-2049, and consequently -36 is correctly calculated.
Change your design! That is the only correct solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 495
1 Sergey Alexandrovich Kryukov 429
2 OriginalGriff 360
3 CHill60 280
4 Sascha Lefévre 269
0 Sergey Alexandrovich Kryukov 10,017
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076

Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100