Home » SQL & PL/SQL » SQL & PL/SQL » difference between dates
difference between dates [message #9725] Tue, 02 December 2003 08:38 Go to next message
omer naj
Messages: 9
Registered: December 2003
Junior Member
hi
how do i create a method whereby it calculates the age in years and days using sysdate and the date_of_birth.

thanks
Re: difference between dates [message #9726 is a reply to message #9725] Tue, 02 December 2003 09:13 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
SQL> CREATE TABLE t (date_of_birth DATE);
  
Table created.
  
SQL> INSERT INTO t VALUES (TO_DATE('19481201','YYYYMMDD'));
SQL> INSERT INTO t VALUES (TO_DATE('19501202','YYYYMMDD'));
SQL> INSERT INTO t VALUES (TO_DATE('19621203','YYYYMMDD'));
SQL> INSERT INTO t VALUES (TO_DATE('19741204','YYYYMMDD'));
SQL> INSERT INTO t VALUES (TO_DATE('19810401','YYYYMMDD'));
sQL> INSERT INTO t VALUES (TO_DATE('19931231','YYYYMMDD'));
SQL> INSERT INTO t VALUES (TO_DATE('19980606','YYYYMMDD'));
SQL> COMMIT;
  
Commit complete.
  
SQL> SELECT TO_CHAR(x.dob,'fmMM/DD/YYYY')            date_of_birth
  2  ,      TO_CHAR(SYSDATE,'fmMM/DD/YYYY')          today
  3  ,      x.age_in_yrs
  4  ,     (TRUNC(SYSDATE)
  5         -
  6         ADD_MONTHS(x.dob, 12 * x.age_in_yrs))    days_remainder
  7  FROM  (SELECT TRUNC(t.date_of_birth) dob
  8         ,      TRUNC(MONTHS_BETWEEN(TRUNC(SYSDATE)
  9                      ,              TRUNC(t.date_of_birth)) / 12) age_in_yrs
 10         FROM   t) x
 11  /
  
DATE_OF_BI TODAY      AGE_IN_YRS DAYS_REMAINDER
---------- ---------- ---------- --------------
12/1/1948  12/2/2003          55              1
12/2/1950  12/2/2003          53              0
12/3/1962  12/2/2003          40            364
12/4/1974  12/2/2003          28            363
4/1/1981   12/2/2003          22            245
12/31/1993 12/2/2003           9            336
6/6/1998   12/2/2003           5            179
  
7 rows selected.
  
SQL>
Happy homeworking,

A.
Re: difference between dates THANKS [message #9728 is a reply to message #9726] Tue, 02 December 2003 09:17 Go to previous message
omer naj
Messages: 9
Registered: December 2003
Junior Member
THANKS
Previous Topic: procedure
Next Topic: reset procedure
Goto Forum:
  


Current Time: Wed Apr 24 18:09:57 CDT 2024