How to calculate a person's age? 
Author Message
 How to calculate a person's age?

Hi all:

I trying to calculate how old are a person. I have the birth date in a
column. I have been using a DATETIME YEAR TO YEAR variable in my program
and asigning to it the result of (TODAY - birthdate) UNITS YEAR it
returns NULL.

I have done using an INTEGER variable without UNITS clause and it
returns the number of days. I know I could divide this number between
365/366 and get the number of years, but I would like to get more
presision.

How have you done this?

--
                               //////
+---------------------------ooo-O-O-ooo--------------------------------+
|                                U                                     |
| Melvin Perez Cedano                          Santo Domingo, Dom. Rep.|

| Systems Development                          Phone (809) 686-5574    |
| CAM Informatica, S. A.                       Fax   (809) 686-5467    |
+----------------------------------------------------------------------+
| ALL RIGHTS RESERVED                                                  |
| This posting is copyright and may be used only for the purposes      |
| of professional study or the professional exchange of information.   |
| Use of this posting by any commercial organisation for the           |
| purposes of vilifying another is explicitly prohibited.              |
+----------------------------------------------------------------------+



Wed, 18 Jun 1902 08:00:00 GMT
 How to calculate a person's age?


Quote:
> I trying to calculate how old are a person. I have the birth date in a
> column. I have been using a DATETIME YEAR TO YEAR variable in my program
> and asigning to it the result of (TODAY - birthdate) UNITS YEAR it
> returns NULL. [...]
> How have you done this?

Well, I hadn't had to do it, but the following seems to work:

CREATE TABLE t
(
    d1 DATETIME YEAR TO DAY NOT NULL,
    d2 DATETIME YEAR TO DAY NOT NULL
);
INSERT INTO t VALUES("1997-09-05", "1990-09-04");
INSERT INTO t VALUES("1997-09-05", "1990-09-06");
INSERT INTO t VALUES("1997-09-05", "1990-09-05");

-- Age in years version...
SELECT d1, d2,
    EXTEND(d1, YEAR TO YEAR) - EXTEND(d2, YEAR TO YEAR) age
    FROM t
    WHERE EXTEND(d1, MONTH TO DAY) >= EXTEND(d2, MONTH TO DAY)
UNION
SELECT d1, d2,
    EXTEND(d1, YEAR TO YEAR) - EXTEND(d2, YEAR TO YEAR) - 1 UNITS YEAR age
    FROM t
    WHERE EXTEND(d1, MONTH TO DAY) < EXTEND(d2, MONTH TO DAY)
;

-- Output
d1|d2|age
DATETIME YEAR TO DAY|DATETIME YEAR TO DAY|INTERVAL YEAR(4) TO YEAR
1997-09-05|1990-09-04|7
1997-09-05|1990-09-05|7
1997-09-05|1990-09-06|6

-- Years and months version...
SELECT d1, d2,
    EXTEND(d1, YEAR TO MONTH) - EXTEND(d2, YEAR TO MONTH) age
    FROM t
    WHERE EXTEND(d1, DAY TO DAY) >= EXTEND(d2, DAY TO DAY)
UNION
SELECT d1, d2,
    EXTEND(d1, YEAR TO MONTH) - EXTEND(d2, YEAR TO MONTH) - 1 UNITS MONTH age
    FROM t
    WHERE EXTEND(d1, DAY TO DAY) < EXTEND(d2, DAY TO DAY)
;

-- Output
d1|d2|age
DATETIME YEAR TO DAY|DATETIME YEAR TO DAY|INTERVAL YEAR(4) TO MONTH
1997-09-05|1990-09-04|7-00
1997-09-05|1990-09-05|7-00
1997-09-05|1990-09-06|6-11

It's a pity that a UNION is necessary -- you could use an SP to do the
calculation with an IF clause (and XPS already has the SQL-92 CASE which
would allow it to be done inline, and CASE will be added to forthcoming
versions of IUS -- and ODS as far as I know).

Yours,



Wed, 18 Jun 1902 08:00:00 GMT
 How to calculate a person's age?

Melvin,
Have you tried this
age=extend(current,year to month)-extend(birthday,year to month)
this however will not be 100% accurate because of the day of the month
but an if statement checking for the day will take care of that.

Joe
-------------------------------------------------------------------------

PO Box 364                  |opinions express are those of my own and
Springdale, AR 72764 USA    |don't necessarily reflect those of my company
-------------------------------------------------------------------------


Quote:
} Hi all:
}
} I trying to calculate how old are a person. I have the birth date in a
} column. I have been using a DATETIME YEAR TO YEAR variable in my program
} and asigning to it the result of (TODAY - birthdate) UNITS YEAR it
} returns NULL.
}
} I have done using an INTEGER variable without UNITS clause and it
} returns the number of days. I know I could divide this number between
} 365/366 and get the number of years, but I would like to get more
} presision.
}
} How have you done this?
}
} --
}                                //////
} +---------------------------ooo-O-O-ooo--------------------------------+
} |                                U                                     |
} | Melvin Perez Cedano                          Santo Domingo, Dom. Rep.|

} | Systems Development                          Phone (809) 686-5574    |
} | CAM Informatica, S. A.                       Fax   (809) 686-5467    |
} +----------------------------------------------------------------------+
} | ALL RIGHTS RESERVED                                                  |
} | This posting is copyright and may be used only for the purposes      |
} | of professional study or the professional exchange of information.   |
} | Use of this posting by any commercial organisation for the           |
} | purposes of vilifying another is explicitly prohibited.              |
} +----------------------------------------------------------------------+
}



Wed, 18 Jun 1902 08:00:00 GMT
 How to calculate a person's age?

I'm very new to Informix (in fact, I haven't even used it) but I've used MS
Access and Oracle7 before, and both of those databases provide a built-in
function that allows you to "subtract" one data from the other and return
the result in different measures of time (days, months, years)

Does Informix offer a similar function?

Eric



Wed, 18 Jun 1902 08:00:00 GMT
 How to calculate a person's age?

Yes.

Several of them.  Judging by the question I'd say you have a lot of reading of
to do.
What interface will you be using (4gl, ESQL/C, New Era, etc) ?

Quote:

} I'm very new to Informix (in fact, I haven't even used it) but I've used MS
} Access and Oracle7 before, and both of those databases provide a built-in
} function that allows you to "subtract" one data from the other and return
} the result in different measures of time (days, months, years)
}
} Does Informix offer a similar function?
}
} Eric



Wed, 18 Jun 1902 08:00:00 GMT
 
 [ 5 post ] 

 Relevant Pages 

1. Calculating person's age from two dates

2. Calculating a person's age from his birthday

3. Calculating Person's Age

4. Calculate a person's age

5. Calculating a person's age knowing their Date of Birth

6. how calculate the age of a person??

7. Calculating age of a person in PL/SQL

8. Dbase IV: How to calculate a persons age...

9. How to determine a person's age

10. how to calculate someone's age

11. calculating today's age

12. Age of a person


 
Powered by phpBB® Forum Software