Click here to Skip to main content
Rate this: bad
good
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.
 
Examples
 
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
Comments
Sudhakar Shinde at 2-Jul-13 7:20am
   
I tried below query which works well..
SELECT ROUND(MONTHS_BETWEEN(SYSDATE,'10-JAN-1949')/12)||' Yrs' FROM dual
 
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
good
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!
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2014
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