【发布时间】:2020-01-05 09:18:07
【问题描述】:
我有以下查询返回开始日期或结束日期在指定日期范围之间的记录。
DECLARE @ReportStartDate date = '01 Apr 2019'
DECLARE @ReportEndDate date = '01 May 2019'
select * from #temp
where
(StartDate between @ReportStartDate and @ReportEndDate) or
(EndDate between @ReportStartDate and @ReportEndDate) or
(EndDate is null)
我遇到的问题是开始日期是“2019 年 3 月 1 日”,结束日期是“2019 年 8 月 4 日”,这意味着它在 2019 年 4 月 1 日和 2019 年 5 月 1 日的日期范围内,但我的标准没有不满足。
我怎样才能包含这些类型的记录?
样本数据:
CREATE TABLE #temp
(
ID int,
StartDate datetime,
EndDate datetime NULL
)
insert into #temp
(
ID,
StartDate,
EndDate
)
select
1,
'01 Mar 2019',
NULL
union all
select
2,
'01 Mar 2019',
'04 Aug 2019'
union all
select
3,
'14 Jul 2019',
NULL
【问题讨论】:
-
那么你期待什么?我同意,
20190301和20190804不在20190401和20190501之间,那么为什么要出现该行呢? -
如果您的
startdate和enddate是20190405和20190412..? -
旁白:
overlap的标签描述解释了适当的测试。
标签: sql sql-server tsql date where-clause