SQL:日期差异是否为负数?

时间:2014-10-26 01:47:26

标签: sql oracle

我的架构看起来像

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

当我尝试从出生日期起age

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

这个时代正在消极,我不知道为什么会这样,有人可以指出问题的问题吗?

由于

我正在使用Oracle数据库

1 个答案:

答案 0 :(得分:2)

我认为,或许你要让它变得比它需要的更困难。尝试:

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

产生结果

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

分享并享受。