如何通过日期过滤排除数据

时间:2018-06-04 14:43:36

标签: sql sql-server date datetime

我需要实现以下逻辑

排除患者日期开始或事件发生前1天的患者:

select patient,[patient date], [event date] 
from stu_tbl 
where [patient date] <> DATEADD(d, - 1, [event date]) and 
      [patient date] <> [event date];
患者日期将是多次

上述条件是否有效?

5 个答案:

答案 0 :(得分:0)

听起来更像你想要的

select patient,
           [patient date],
           [event date]
           from stu_tbl
           where [patient date] >= DATEADD(d, - 1, [event date])
                      and [patient date] < cast([event date] AS date);

答案 1 :(得分:0)

为了添加多个患者日期,您可以使用IN运算符 在哪里条件。 像是

select patient,[patient date], [event date] 
from stu_tbl 
where [patient date] NOT IN (patient date1, patient date2)

答案 2 :(得分:0)

试试这个:

    select patient,
       [patient date],
       [event date]
       from stu_tbl
       where [patient date] NOT IN (DATEADD(d, - 1, [event date]), [event date])

答案 3 :(得分:0)

试试这个

select patient,[patient date], [event date] 
from stu_tbl 
where DateDiff(Day, [patient date], [event date]) < 0

答案 4 :(得分:0)

您的日期可能有时间成分。如果是这样的话:

select patient, [patient date], [event date] 
from stu_tbl 
where [patient date] <  dateadd(day, -1, cast([event date] as date)) or 
      [patient date] >= dateadd(day, 1, cast([event date] as date));