SQL语法-过滤日期

时间:2019-03-05 01:36:45

标签: sql sql-server

我有以下SQL输出数据。

-----------------------------------------------------------------------------
| PymtDate  | ChkInDate | ChkOutDate| OtherDetails                          |
-----------------------------------------------------------------------------
| 2018-06-10| NULL      | NULL      | qqqqq                                 |
-----------------------------------------------------------------------------
| 2018-07-01| NULL      | NULL      | sssss                                 |
-----------------------------------------------------------------------------
| 2018-07-26|2019-02-10 |2018-08-10 | xxxxx                                 |
-----------------------------------------------------------------------------
| 2018-11-16|2019-01-29 |2018-12-29 | yyyyy                                 |
-----------------------------------------------------------------------------
| 2018-11-20|2019-01-01 |2019-01-01 | zzzzz                                 |
-----------------------------------------------------------------------------
| 2018-11-20|2019-01-02 |2019-01-01 | aaaaa                                 |
-----------------------------------------------------------------------------

它是从以下SQL生成的。

select CONVERT(char(10), DatePayment, 120) PymtDate, CONVERT(char(10), DateCheckIn, 120) ChkInDate, CONVERT(char(10), DateCheckOut, 120) ChkOutDate, details as OtherDetails from details 
where PymtDate<= 'asAtDate' and 
ChkInDate>= DATEADD(DD,1,'asAtDate') or 
ChkOutDate>= DATEADD(DD,1,'asAtDate');
PymtDate的

asAtDate在这种情况下是2018年12月31日。

要求是删除在atAtDate之后的ChkInDate和ChkOutDate中的所有数据(在本例中为2018-12-31之后)。如果将AND放在where条件下,它将删除具有NULL值的ChkInDate和ChkOutDate的数据。

当前SQL仍显示ChkOutDate ..的数据2018,如果ChkInDate获得了2018数据..由于OR条件,它仍显示..我该如何解决?请帮忙..谢谢

BTW .. im使用SQL Server Mgmt Studio。编辑了SQL以更好地理解。

我的预期结果如下:

-----------------------------------------------------------------------------
| PymtDate  | ChkInDate | ChkOutDate| OtherDetails                          |
-----------------------------------------------------------------------------
| 2018-06-10| NULL      | NULL      | qqqqq                                 |
-----------------------------------------------------------------------------
| 2018-07-01| NULL      | NULL      | sssss                                 |
-----------------------------------------------------------------------------
| 2018-11-20|2019-01-01 |2019-01-01 | zzzzz                                 |
-----------------------------------------------------------------------------
| 2018-11-20|2019-01-02 |2019-01-01 | aaaaa                                 |
-----------------------------------------------------------------------------

4 个答案:

答案 0 :(得分:0)

您可以在下面尝试-

select Date1, Date2, Date3, details as OtherDetails from details 
where Date1 <= 'asAtDate' and 
(
  (Date3 > DATEADD(DD,1,'asAtDate') or Date3 is null) and 
  (Date2 > DATEADD(DD,1,'asAtDate') or Date2 is null)
)

答案 1 :(得分:0)

这会满足您的要求吗?

select Date1, Date2, Date3, details as OtherDetails from details 
where Date1 <= 'asAtDate' and 
    (Date3 > DATEADD(DD,1,'asAtDate') or Date2 > DATEADD(DD,1,'asAtDate'));

答案 2 :(得分:0)

如果您想要在日期之后的 值,为什么不这样做:

where Date1 <= @asAtDate and 
      (Date3 > @asAtDate or Date3 is null) and
      (Date2 > @asAtDate or Date2 is null)

我不明白您为什么要在值中加上一天,然后使用>

'asAtDate'的使用(对我而言)表明您正在将值直接插入查询字符串中。那是个坏主意。 @asAtDate建议使用参数。

答案 3 :(得分:0)

我用以下命令修复了SQL。 感谢大家的评论和帮助。真的很感激。 再次感谢。

select CONVERT(char(10), DatePayment, 120) PymtDate, CONVERT(char(10), DateCheckIn, 120) ChkInDate, CONVERT(char(10), DateCheckOut, 120) ChkOutDate, details as OtherDetails from details 
where PymtDate <= 'asAtDate' and 
(isNull(ChkInDate,ChkOutDate) >= 'asAtDate' and ChkOutDate >= 'asAtDate');