dbTalk Databases Forums  

SCRIPT: Get age of person in Years, Months, Days.

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss SCRIPT: Get age of person in Years, Months, Days. in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Craig & Co.
 
Posts: n/a

Default SCRIPT: Get age of person in Years, Months, Days. - 02-16-2005 , 10:42 PM






Hi,

I have written a script that will get the age of a person in Years, Months
and Days, based
on the sysdate and date_of_birth in a table.

Enjoy - If anyone can see an where to improve code, let me know too.

The first part I did pinch from another lister: Kimmy Chan - posted
27-May-1998.

Cheers
Craig.

select id_number,
given_name,
family_name,
date_of_birth,
/* get estimated age first */
to_char(sysdate,'yyyy')-to_char(date_of_birth,'yyyy') -
/* adjust the age according to the month and day */
decode(sign(to_char(sysdate,'mm')-
to_char(date_of_birth,'mm')),
-1,1, /* earlier month */
+1,0, /* later month */
/* same month, then determine by the day part */
0,decode(sign(to_char(sysdate,'dd')-
to_char(date_of_birth,'dd')),
-1,1, /* earlier day */
0 /* later or same day */
)) "Years",
decode(abs(to_char(date_of_birth,'mm') - to_char(sysdate,'mm') -
12),12,0,
abs(to_char(date_of_birth,'mm') - to_char(sysdate,'mm') - 12))
"Months",
decode(round(sysdate -
to_date(to_char(to_char(date_of_birth,'DD/MM')||'/'||
to_number(to_char(sysdate,'YYYY') - 1)),'DD/MM/YYYY'),0),
decode (mod(to_char(sysdate,'yyyy'),4),0,366,367),0,
round(sysdate -
to_date(to_char(to_char(date_of_birth,'DD/MM')||'/'||to_number(
to_char(sysdate,'YYYY') - 1)),'DD/MM/YYYY'),0)) "Days"
from emp_det
order by date_of_birth, family_name
/




Reply With Quote
  #2  
Old   
Craig & Co.
 
Posts: n/a

Default Re: SCRIPT: Get age of person in Years, Months, Days. - 02-16-2005 , 11:03 PM






Made it a little more complicated by allowing users to enter any date.
col date_of_birth format a14
select '&&date_of_birth' "Date_Of_Birth",
/* get estimated age first */

to_char(sysdate,'yyyy')-to_char(to_date('&date_of_birth'),'yyyy') -
/* adjust the age according to the month and day */
decode(sign(to_char(sysdate,'mm')-
to_char(to_date('&date_of_birth'),'mm')),
-1,1, /* earlier month */
+1,0, /* later month */
/* same month, then determine by the day part */
0,decode(sign(to_char(sysdate,'dd')-
to_char(to_date('&date_of_birth'),'dd')),
-1,1, /* earlier day */
0 /* later or same day */
)) "Years",
decode(abs(to_char(to_date('&date_of_birth'),'mm') -
to_char(sysdate,'mm') -
12),12,0,abs(to_char(to_date('&date_of_birth'),'mm ') -
to_char(sysdate,'mm') - 12)) "Months",
decode(round(sysdate - to_date(to_char(to_char(

to_date('&date_of_birth'),'DD/MM')||'/'||to_number(to_char(sysdate,'YYYY') -
1)),'DD/MM/YYYY'),0), decode
(mod(to_char(sysdate,'yyyy'),4),0,366,367)
,0,round(sysdate - to_date(to_char(to_char(
to_date('&date_of_birth'),'DD/MM')||'/'||to_number(
to_char(sysdate,'YYYY') - 1)),'DD/MM/YYYY'),0)) - 1
"Days"
from dual
/
undefine date_of_birth


Cheers
Craig

"Craig & Co." <crb (AT) amsa (DOT) gov.au> wrote


Quote:
Hi,

I have written a script that will get the age of a person in Years, Months
and Days, based
on the sysdate and date_of_birth in a table.

Enjoy - If anyone can see an where to improve code, let me know too.

The first part I did pinch from another lister: Kimmy Chan - posted
27-May-1998.

Cheers
Craig.

select id_number,
given_name,
family_name,
date_of_birth,
/* get estimated age first */
to_char(sysdate,'yyyy')-to_char(date_of_birth,'yyyy') -
/* adjust the age according to the month and day */
decode(sign(to_char(sysdate,'mm')-
to_char(date_of_birth,'mm')),
-1,1, /* earlier month */
+1,0, /* later month */
/* same month, then determine by the day part */
0,decode(sign(to_char(sysdate,'dd')-
to_char(date_of_birth,'dd')),
-1,1, /* earlier day */
0 /* later or same day */
)) "Years",
decode(abs(to_char(date_of_birth,'mm') - to_char(sysdate,'mm') -
12),12,0,
abs(to_char(date_of_birth,'mm') - to_char(sysdate,'mm') - 12))
"Months",
decode(round(sysdate -
to_date(to_char(to_char(date_of_birth,'DD/MM')||'/'||
to_number(to_char(sysdate,'YYYY') - 1)),'DD/MM/YYYY'),0),
decode (mod(to_char(sysdate,'yyyy'),4),0,366,367),0,
round(sysdate -
to_date(to_char(to_char(date_of_birth,'DD/MM')||'/'||to_number(
to_char(sysdate,'YYYY') - 1)),'DD/MM/YYYY'),0)) "Days"
from emp_det
order by date_of_birth, family_name
/






Reply With Quote
  #3  
Old   
Michel Cadot
 
Posts: n/a

Default Re: SCRIPT: Get age of person in Years, Months, Days. - 02-16-2005 , 11:34 PM




"Craig & Co." <crb (AT) amsa (DOT) gov.au> a écrit dans le message de
news:421420c4$0$57505$c30e37c6 (AT) ken-reader (DOT) news.telstra.net...
Quote:
Hi,

I have written a script that will get the age of a person in Years, Months
and Days, based
on the sysdate and date_of_birth in a table.

Enjoy - If anyone can see an where to improve code, let me know too.

The first part I did pinch from another lister: Kimmy Chan - posted
27-May-1998.

Cheers
Craig.

select id_number,
given_name,
family_name,
date_of_birth,
/* get estimated age first */
to_char(sysdate,'yyyy')-to_char(date_of_birth,'yyyy') -
/* adjust the age according to the month and day */
decode(sign(to_char(sysdate,'mm')-
to_char(date_of_birth,'mm')),
-1,1, /* earlier month */
+1,0, /* later month */
/* same month, then determine by the day part */
0,decode(sign(to_char(sysdate,'dd')-
to_char(date_of_birth,'dd')),
-1,1, /* earlier day */
0 /* later or same day */
)) "Years",
decode(abs(to_char(date_of_birth,'mm') - to_char(sysdate,'mm') -
12),12,0,
abs(to_char(date_of_birth,'mm') - to_char(sysdate,'mm') - 12))
"Months",
decode(round(sysdate -
to_date(to_char(to_char(date_of_birth,'DD/MM')||'/'||
to_number(to_char(sysdate,'YYYY') - 1)),'DD/MM/YYYY'),0),
decode (mod(to_char(sysdate,'yyyy'),4),0,366,367),0,
round(sysdate -
to_date(to_char(to_char(date_of_birth,'DD/MM')||'/'||to_number(
to_char(sysdate,'YYYY') - 1)),'DD/MM/YYYY'),0)) "Days"
from emp_det
order by date_of_birth, family_name
/


Why don't use Oracle built-in functions?

For date_of_date enter in DD/MM/YYYY format:

select trunc(months_between(sysdate,to_date('&&date_of_bi rth','MM/DD/YYYY'))/12)
Quote:
| ' years ' ||
mod(trunc(months_between(sysdate, to_date('&&date_of_birth','MM/DD/YYYY'))),12)
| ' months ' ||
trunc(sysdate
- add_months(to_date('&&date_of_birth','MM/DD/YYYY'),
months_between(sysdate, to_date('&&date_of_birth','MM/DD/YYYY'))))
Quote:
| ' days' "Year_Month_Day"
from dual;

Regards
Michel Cadot




Reply With Quote
  #4  
Old   
Craig & Co.
 
Posts: n/a

Default Re: SCRIPT: Get age of person in Years, Months, Days. - 02-17-2005 , 04:31 PM



"Michel Cadot" <micadot{at}altern{dot}org> wrote

Quote:

Why don't use Oracle built-in functions?

For date_of_date enter in DD/MM/YYYY format:

select
trunc(months_between(sysdate,to_date('&&date_of_bi rth','MM/DD/YYYY'))/12)
|| ' years ' ||
mod(trunc(months_between(sysdate,
to_date('&&date_of_birth','MM/DD/YYYY'))),12)
|| ' months ' ||
trunc(sysdate
- add_months(to_date('&&date_of_birth','MM/DD/YYYY'),
months_between(sysdate,
to_date('&&date_of_birth','MM/DD/YYYY'))))
|| ' days' "Year_Month_Day"
from dual;

Regards
Michel Cadot


Thanks for that. That's a lot neater for the Years and Months, however, the
days is wrong.

Using my birthday - 07-Apr-1975 - It comes up with 29 years 10 months and 10
days.
The Days should at least be 300+.
Hence the huge last statement in the program. I do like what you've done
with the Years and Months though
and will change my program accordingly.

Cheers
Craig.




Reply With Quote
  #5  
Old   
Michel Cadot
 
Posts: n/a

Default Re: SCRIPT: Get age of person in Years, Months, Days. - 02-18-2005 , 11:06 AM




"Craig & Co." <crb (AT) amsa (DOT) gov.au> a écrit dans le message de
news:42151b79$0$57510$c30e37c6 (AT) ken-reader (DOT) news.telstra.net...
Quote:
"Michel Cadot" <micadot{at}altern{dot}org> wrote in message
news:42142cbb$0$10013$626a14ce (AT) news (DOT) free.fr...


Why don't use Oracle built-in functions?

For date_of_date enter in DD/MM/YYYY format:

select
trunc(months_between(sysdate,to_date('&&date_of_bi rth','MM/DD/YYYY'))/12)
|| ' years ' ||
mod(trunc(months_between(sysdate,
to_date('&&date_of_birth','MM/DD/YYYY'))),12)
|| ' months ' ||
trunc(sysdate
- add_months(to_date('&&date_of_birth','MM/DD/YYYY'),
months_between(sysdate,
to_date('&&date_of_birth','MM/DD/YYYY'))))
|| ' days' "Year_Month_Day"
from dual;

Regards
Michel Cadot


Thanks for that. That's a lot neater for the Years and Months, however, the
days is wrong.

Using my birthday - 07-Apr-1975 - It comes up with 29 years 10 months and 10
days.
The Days should at least be 300+.
I don't understand "300+": number of days is always between 1 and number of days in the current
month minus 1.
This is the number of days you have to add to your 29 years and 10 months to get back to your birth
day.

Quote:
Hence the huge last statement in the program. I do like what you've done
with the Years and Months though
and will change my program accordingly.

Cheers
Craig.

Regards
Michel Cadot




Reply With Quote
  #6  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: SCRIPT: Get age of person in Years, Months, Days. - 02-21-2005 , 03:44 AM



Michel Cadot wrote:
Quote:
"Craig & Co." <crb (AT) amsa (DOT) gov.au> a écrit dans le message de
news:42151b79$0$57510$c30e37c6 (AT) ken-reader (DOT) news.telstra.net...
| "Michel Cadot" <micadot{at}altern{dot}org> wrote in message
| news:42142cbb$0$10013$626a14ce (AT) news (DOT) free.fr...
|
|
| > Why don't use Oracle built-in functions?
|
| > For date_of_date enter in DD/MM/YYYY format:
|
| > select
| trunc(months_between(sysdate,to_date('&&date_of_bi rth','MM/DD/YYYY'))/12)
| > || ' years ' ||
| > mod(trunc(months_between(sysdate,
| to_date('&&date_of_birth','MM/DD/YYYY'))),12)
| > || ' months ' ||
| > trunc(sysdate
| > - add_months(to_date('&&date_of_birth','MM/DD/YYYY'),
| > months_between(sysdate,
| to_date('&&date_of_birth','MM/DD/YYYY'))))
| > || ' days' "Year_Month_Day"
| > from dual;
|
| > Regards
| > Michel Cadot
|
|
| Thanks for that. That's a lot neater for the Years and Months, however, the
| days is wrong.
|
| Using my birthday - 07-Apr-1975 - It comes up with 29 years 10 months and 10
| days.
| The Days should at least be 300+.

I don't understand "300+": number of days is always between 1 and number of days in the current
month minus 1.
This is the number of days you have to add to your 29 years and 10 months to get back to your birth
day.

| Hence the huge last statement in the program. I do like what you've done
| with the Years and Months though
| and will change my program accordingly.
|
| Cheers
| Craig.
|

Regards
Michel Cadot


April, 7th is *not* 300+ days into the year, but day 97.

--
Regards,
Frank van Bortel


Reply With Quote
  #7  
Old   
Craig & Co.
 
Posts: n/a

Default Re: SCRIPT: Get age of person in Years, Months, Days. - 02-21-2005 , 03:28 PM



Quote:
I don't understand "300+": number of days is always between 1 and number
of days in the current
month minus 1.
This is the number of days you have to add to your 29 years and 10 months
to get back to your birth
day.

I do apologies. Maths was never my strong point. 300+ days is actually 29
years and 300+ days, where
you were right it should be 29 years 10 months and how many days into this
month.

Will alter code appropriately.

Craig




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.