计算出生日期SQL DB2并指示坏数据

问题描述 投票:0回答:4

我正在尝试计算学生入学时的年龄。我已经尝试过这个查询,但在某些情况下,我的入院日期早于他们的出生日期(错误数据)。

例如:

date of birth is 2016-10-22-00.00.00.000000 
admission date is 2016-09-09-14.51.00.000000 

我得到了 0,但我应该得到带有 (-) 负数的东西,这样我就可以看到这是坏数据。谢谢你

SELECT * 
FROM   (SELECT student_id, 
               Timestampdiff(256, Char(Timestamp(admission_date) - 
                                       Timestamp(date_of_birth))) 
               AS Admission_DOB 
        FROM   person) AS a 
WHERE  admission_dob <= '10' 
sql db2 subquery
4个回答
0
投票

您应该将其作为单独的问题来解决。一个用于计算年龄,另一个用于查找不良数据。

因此查找不良数据应该更简单,例如:

SELECT *, 'good data', age(start, end) as age
FROM  person
WHERE admission_date > date_of_birth

UNION ALL

SELECT *, 'bad data', null as age -- or age(end, start)
FROM  person
WHERE admission_date < date_of_birth

0
投票
SELECT student_id, 
   CASE admission_date > date_of_birth THEN 
     Timestampdiff(256, Char(Timestamp(admission_date)-Timestamp(date_of_birth))) 

   ELSE 
      Timestampdiff(256, Char(Timestamp(date_of_birth)-Timestamp(admission_date))) 

           AS Admission_DOB 
    FROM   person

0
投票
SELECT * 
FROM   (SELECT student_id,  
               CASE WHEN  ar.DT_ACCESS_RCVD <  p.DT_BRTH 
                    THEN '-9' 
                    ELSE  Timestampdiff(256, Char(Timestamp(admission_date) - 
                                                  Timestamp(date_of_birth))) 
               END AS Admission_DOB 
        FROM   person) AS a 
WHERE  admission_dob <= '10' 

0
投票

YEARS_BETWEEN(当前日期,PERSON.BIRTH_DATE)

© www.soinside.com 2019 - 2024. All rights reserved.