-1

My schema looks like

select dob, name from student;

DOB        NAME
---------  -------
01-APR-68  rylan
01-APR-70  joshua
01-APR-83  ava
01-APR-76  adalgisa
01-APR-83  zaida
01-APR-83  perry
01-APR-82  aiden
01-APR-75  madison
01-APR-62  zoe
01-APR-72  brendon
01-APR-75  cayden
01-APR-83  hannah

When I try to get age from date of birth as

SELECT DISTINCT s.NAME,
                s.ssn,
                s.address.streetName                                   AS street,
                ( Floor (Months_between(Trunc(sysdate), s.dob) / 12) ) AS age
FROM   student s,
       student s1
WHERE  s.dob = (SELECT Max(DISTINCT dob)
                FROM   student); 


NAME         SSN            STREET                    AGE
-----       --------      ------------                ---
perry       111110003     union square                -69

zaida       111110000     5th street                  -69

hannah      111111111     union square                -69

ava         111118888     5th street                  -69

The age is coming as negative, I have no idea why this is happening, can someone please point out the issue with the query?

Thanks

I am using Oracle Database

user272735
  • 10,473
  • 9
  • 65
  • 96
daydreamer
  • 87,243
  • 191
  • 450
  • 722

1 Answers1

2

I think that perhaps you're making this a bit more difficult than it needs to be. Try:

SELECT s.NAME,
       trunc(months_between(Trunc(sysdate), s.dob)/12) AS age
  FROM student s;

which produces the result

NAME        AGE
rylan       46
joshua      44
ava         31
adalgisa    38
zaida       31
perry       31
aiden       32
madison     39
zoe         52
brendon     42
cayden      39
hannah      31

SQLFiddle here

Share and enjoy.