Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old July 21st, 2004, 10:32 AM
rynorem rynorem is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 rynorem User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 29 m 57 sec
Reputation Power: 0
converting date of birth to age inSQLPLUS

In our database we keep the date of birth of members. I need to convert those dates to age. I'm not having alot of luck. We use SQLPLUS. Any help is appreciated.

Reply With Quote
  #2  
Old July 21st, 2004, 12:18 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
try something like:
Code:
SELECT ROUND( (SYSDATE - DOB)/365.24,0) FROM MYTABLE; 


365.24 = days in a year

Reply With Quote
  #3  
Old July 21st, 2004, 06:51 PM
gamyers gamyers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 14 gamyers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Try the following :

select bth_date, to_number(to_char(sysdate,'YYYY')) - to_number(to_char(bth_date,'YYYY')) +
decode(sign(to_number(to_char(sysdate,'MMDD')) - to_number(to_char(bth_date,'MMDD'))),-1,-1,0) age
from (select to_date('01/jan/1994','dd/mon/yyyy') bth_date from dual
union select to_date('01/jul/1994','dd/mon/yyyy') bth_date from dual
union select to_date('22/jul/1994','dd/mon/yyyy') bth_date from dual
union select to_date('23/jul/1994','dd/mon/yyyy') bth_date from dual
union select to_date('24/jul/1994','dd/mon/yyyy') bth_date from dual
union select to_date('31/dec/1994','dd/mon/yyyy') bth_date from dual
union select to_date('01/jan/2004','dd/mon/yyyy') bth_date from dual
union select to_date('01/jan/2005','dd/mon/yyyy') bth_date from dual
)

Reply With Quote
  #4  
Old July 22nd, 2004, 10:11 PM
swstephe swstephe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Location: Brunei, Borneo Island, South East Asia
Posts: 9 swstephe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to swstephe
Something like Jim's is best, however, I think most people only measure their age from their last birthday, (rather than rounding):

select trunc((sysdate - dob)/365.24) from mytable;

you could use gamyers version without all the conversions:

select to_char(sysdate,'YYYY') - to_char(bth_date,'YYYY') from mytable;

(the "-" will make SQL automatically convert to number). But, again, everyone will get their birthday on January 1st.

Reply With Quote
  #5  
Old July 25th, 2004, 08:48 PM
gamyers gamyers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 14 gamyers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It depends how precise you need to be.
The 'trunc' solution can give some dodgy results when run on a person's birthday.
For example, if the calculation is run on '31-jan-2004' and a person's birthdate was '31-jan-2001', then the trunc version gives an age of 2, whereas mine gives an age of 3.

Bet you the child concerned will be loudly telling you that "I'm three today" though.

Because the ".24" doesn't reflect that we have an extra day every four years, rather than a quarter day every year, I think it will underestimate the age between 1% and 2% of the time. You could play with the script below to work out the precise percentage....

create table x (birth_date date , current_date date, first_age_calc number, second_age_cal number)

declare
v_start_date date := to_date('01-jan-2000','dd-mon-yyyy');
v_end_date date := to_date('31-dec-2004','dd-mon-yyyy');
begin
while v_start_date <= v_end_date loop
if to_number(to_char(v_start_date,'DD')) = 1
or v_start_date = last_day(v_start_date) then
insert into x (birth_date) values (v_start_date);
end if;
v_start_date := v_start_date + 1;
end loop;
end;

insert into x (birth_date, current_date)
select a.birth_date, b.birth_date
from x a, x b
where a.birth_date <= b.birth_date

delete from x
where current_date is null

update x set first_age_calc = trunc((current_date - birth_date)/365.24),
second_age_cal =
to_number(to_char(current_date,'YYYY')) - to_number(to_char(birth_date,'YYYY')) +
decode(sign(to_number(to_char(current_date,'MMDD')) - to_number(to_char(birth_date,'MMDD'))),-1,-1,0)


delete from x where first_age_calc = second_age_cal
(7119 rows deleted, leaving 141)

Reply With Quote
  #6  
Old July 28th, 2004, 01:06 PM
KK2796 KK2796 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 11 KK2796 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 40 sec
Reputation Power: 0
Quote:
Originally Posted by rynorem
In our database we keep the date of birth of members. I need to convert those dates to age. I'm not having alot of luck. We use SQLPLUS. Any help is appreciated.

-- Not making a mountain of a molehill....
SELECT
TO_CHAR(SYSDATE,'YYYY') - TO_CHAR(:bd,'YYYY') -
CASE WHEN TO_CHAR(SYSDATE,'MMDD') < TO_CHAR(:BD,'MMDD') THEN 1 ELSE 0 END
FROM DUAL

EDIT: yipes - just realized this was identical to gamyers solution. Nevermind!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > converting date of birth to age inSQLPLUS


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway