Link to home
Start Free TrialLog in
Avatar of JCTDD
JCTDD

asked on

Informix SQL get age from ddmmyyyy field

The code below is not accurate as it only minuses the year

year(today)-year(DOB) as AGE

does anybody know a more accurate way of returing a persons age taking into account the day and month of the year as well?


Avatar of jhp333
jhp333
Flag of United States of America image

1. Simple, but approximate solution:
select ((today - DOB) / 365.25)::int

2. Accurate one:
select year(today) - year(DOB) - case when mdy(month(DOB),day(DOB),year(today)) > today then 1 else 0 end
Avatar of JCTDD
JCTDD

ASKER

running 2. Accurate one: in aqua studio I get error:

"Invalid day in date"

select year(today) - year(mailing_code) - case when mdy(month(mailing_code),day(mailing_code),year(today)) > today then 1 else 0 end
from table_1

Open in new window


do you know what is wrong?
Avatar of JCTDD

ASKER

example DOBs from the mailing_code field:
mailing_code
26/03/1941
1/10/1958
21/07/1961
ASKER CERTIFIED SOLUTION
Avatar of jhp333
jhp333
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial