如何查找 table 是否具有给定的日期范围
How to find whether a table has a given date range
我想查明 table 是否有在传递给查询的日期范围内的行。以下是MYTABLE
中的数据。我正在使用 SQL 服务器
DISPLAY_START_DATE DISPLAY_END_DATE
2022-02-02 00:00:00.000 2022-02-28 00:00:00.000
2022-02-02 00:00:00.000 2022-02-06 10:34:01.653
2022-02-01 00:00:00.000 2022-02-17 00:00:00.000
2022-02-07 00:00:00.000 2022-02-25 00:00:00.000
以下是我的查询
DECLARE @startdate AS datetime ='2022-02-01'
DECLARE @enddate AS datetime ='2022-02-10'
SELECT * from MYTABLE mt
WHERE
(mt.DISPLAY_START_DATE = @startdate and mt.DISPLAY_END_DATE = @enddate) OR
(mt.DISPLAY_START_DATE < @startdate and mt.DISPLAY_END_DATE > @enddate) OR
(mt.DISPLAY_START_DATE < @startdate and mt.DISPLAY_END_DATE < @enddate) OR
(mt.DISPLAY_START_DATE < @startdate and mt.DISPLAY_END_DATE < @enddate and
mt.DISPLAY_END_DATE > @startdate) OR
(mt.DISPLAY_START_DATE > @startdate and mt.DISPLAY_END_DATE < @enddate) OR
(mt.DISPLAY_START_DATE > @startdate and mt.DISPLAY_START_DATE < @enddate and
mt.DISPLAY_END_DATE < @enddate)
这里只拉取对应以下数据的第二行
DISPLAY_START_DATE DISPLAY_END_DATE
2022-02-02 00:00:00.000 2022-02-06 10:34:01.653
两个区间相交的条件是每个区间必须先于其他区间开始:
..
WHERE mt.DISPLAY_START_DATE <= @enddate AND @startdate <= mt.DISPLAY_END_DATE
如果你想 select 给定范围内的所有行,你可以尝试下面的查询:
DECLARE @startdate AS datetime ='2022-02-01'
DECLARE @enddate AS datetime ='2022-02-10'
SELECT * from MYTABLE mt
WHERE
(mt.DISPLAY_START_DATE between @startdate and @enddate and mt.DISPLAY_END_DATE between @startdate and @enddate)
或者如果您想要一个 where 条件 select table 中完全或部分在范围
中的所有行
SELECT * from MYTABLE mt
WHERE
(mt.DISPLAY_START_DATE between @startdate and @enddate and mt.DISPLAY_END_DATE between @startdate and @enddate) or
(mt.DISPLAY_START_DATE <=@startdate and mt.DISPLAY_END_DATE >=@enddate) or
(mt.DISPLAY_START_DATE <=@startdate and mt.DISPLAY_END_DATE between @startdate and @enddate) or
(mt.DISPLAY_START_DATE between @startdate and @enddate and mt.DISPLAY_END_DATE >=@enddate)
我想查明 table 是否有在传递给查询的日期范围内的行。以下是MYTABLE
中的数据。我正在使用 SQL 服务器
DISPLAY_START_DATE DISPLAY_END_DATE
2022-02-02 00:00:00.000 2022-02-28 00:00:00.000
2022-02-02 00:00:00.000 2022-02-06 10:34:01.653
2022-02-01 00:00:00.000 2022-02-17 00:00:00.000
2022-02-07 00:00:00.000 2022-02-25 00:00:00.000
以下是我的查询
DECLARE @startdate AS datetime ='2022-02-01'
DECLARE @enddate AS datetime ='2022-02-10'
SELECT * from MYTABLE mt
WHERE
(mt.DISPLAY_START_DATE = @startdate and mt.DISPLAY_END_DATE = @enddate) OR
(mt.DISPLAY_START_DATE < @startdate and mt.DISPLAY_END_DATE > @enddate) OR
(mt.DISPLAY_START_DATE < @startdate and mt.DISPLAY_END_DATE < @enddate) OR
(mt.DISPLAY_START_DATE < @startdate and mt.DISPLAY_END_DATE < @enddate and
mt.DISPLAY_END_DATE > @startdate) OR
(mt.DISPLAY_START_DATE > @startdate and mt.DISPLAY_END_DATE < @enddate) OR
(mt.DISPLAY_START_DATE > @startdate and mt.DISPLAY_START_DATE < @enddate and
mt.DISPLAY_END_DATE < @enddate)
这里只拉取对应以下数据的第二行
DISPLAY_START_DATE DISPLAY_END_DATE
2022-02-02 00:00:00.000 2022-02-06 10:34:01.653
两个区间相交的条件是每个区间必须先于其他区间开始:
..
WHERE mt.DISPLAY_START_DATE <= @enddate AND @startdate <= mt.DISPLAY_END_DATE
如果你想 select 给定范围内的所有行,你可以尝试下面的查询:
DECLARE @startdate AS datetime ='2022-02-01'
DECLARE @enddate AS datetime ='2022-02-10'
SELECT * from MYTABLE mt
WHERE
(mt.DISPLAY_START_DATE between @startdate and @enddate and mt.DISPLAY_END_DATE between @startdate and @enddate)
或者如果您想要一个 where 条件 select table 中完全或部分在范围
中的所有行SELECT * from MYTABLE mt
WHERE
(mt.DISPLAY_START_DATE between @startdate and @enddate and mt.DISPLAY_END_DATE between @startdate and @enddate) or
(mt.DISPLAY_START_DATE <=@startdate and mt.DISPLAY_END_DATE >=@enddate) or
(mt.DISPLAY_START_DATE <=@startdate and mt.DISPLAY_END_DATE between @startdate and @enddate) or
(mt.DISPLAY_START_DATE between @startdate and @enddate and mt.DISPLAY_END_DATE >=@enddate)