127

我有一个名为的表Product_Sales,它包含这样的数据

Product_ID | Sold_by | Qty | From_date  | To_date
-----------+---------+-----+------------+-----------
3          | 12      | 7   | 2013-01-05 | 2013-01-07
6          | 22      | 14  | 2013-01-06 | 2013-01-10
8          | 11      | 9   | 2013-02-05 | 2013-02-11

现在,如果我想从日期范围中选择两个日期之间的销售数据,查询是什么?

例如,我想从 to 中选择销售2013-01-03数据2013-01-09

4

19 回答 19

188

interval intersection description

As you can see, there are two ways to get things done:

  • enlist all acceptable options
  • exclude all wrong options

Obviously, second way is much more simple (only two cases against four).

Your SQL will look like:

SELECT * FROM Product_sales 
WHERE NOT (From_date > @RangeTill OR To_date < @RangeFrom)
于 2014-07-27T18:56:47.873 回答
111
SELECT * from Product_sales where
(From_date BETWEEN '2013-01-03'AND '2013-01-09') OR 
(To_date BETWEEN '2013-01-03' AND '2013-01-09') OR 
(From_date <= '2013-01-03' AND To_date >= '2013-01-09')

你必须涵盖所有的可能性。From_DateTo_Date可能在您的日期范围之间,或者记录日期可能涵盖整个范围。

如果其中一个From_dateTo_date在日期之间,或者From_date小于开始日期并且To_date大于结束日期;那么应该返回这一行。

于 2016-04-01T10:45:16.037 回答
45

尝试以下查询以获取范围之间的日期:

SELECT  *
FROM    Product_sales 
WHERE   From_date >= '2013-01-03' AND
        To_date   <= '2013-01-09'
于 2013-01-08T05:53:32.923 回答
26
SELECT * FROM Product_sales 
WHERE From_date between '2013-01-03'
AND '2013-01-09'
于 2013-01-08T06:00:30.393 回答
8
SELECT *
FROM Product_sales
WHERE (
From_date >= '2013-08-19'
AND To_date <= '2013-08-23'
)
OR (
To_date >= '2013-08-19'
AND From_date <= '2013-08-23'
)
于 2013-08-20T10:18:48.193 回答
8

这涵盖了您正在寻找的所有条件。

SELECT * from Product_sales where (From_date <= '2013-01-09' AND To_date >= '2013-01-01')
于 2019-08-07T12:43:25.770 回答
6

请试试:

DECLARE @FrmDt DATETIME, @ToDt DATETIME
SELECT @FrmDt='2013-01-03', @ToDt='2013-01-09'

SELECT * 
FROM Product_sales 
WHERE (@FrmDt BETWEEN From_date AND To_date) OR 
    (@ToDt BETWEEN From_date AND To_date)
于 2013-01-08T06:18:00.143 回答
4

只需我的 2 美分,我发现使用“dd-MMM-yyyy”格式最安全,因为无论服务器上的区域设置如何,数据库服务器都会知道您想要什么。否则,您可能会在日期区域设置为 yyyy-dd-mm 的服务器上遇到问题(无论出于何种原因)

因此:

SELECT * FROM Product_sales 
WHERE From_date >= '03-Jan-2013'
AND To_date <= '09-Jan-2013'

它对我来说总是很有效;-)

于 2013-09-30T14:53:40.383 回答
4
select * 
from table 
where
( (table.EndDate > '2013-01-05') and (table.StartDate < '2013-01-07' )  )
于 2016-10-07T19:26:30.680 回答
3

检查这个查询,我创建了这个查询来检查入住日期是否与任何预订日期重叠

SELECT * FROM tbl_ReservedRooms
WHERE NOT ('@checkindate' NOT BETWEEN fromdate AND todate
  AND '@checkoutdate'  NOT BETWEEN fromdate AND todate)

这将重新运行重叠的详细信息,以获取不重叠的详细信息,然后从查询中删除“NOT”

于 2016-04-01T10:28:00.800 回答
3

此查询将帮助您:

select * 
from XXXX
where datepart(YYYY,create_date)>=2013 
and DATEPART(YYYY,create_date)<=2014
于 2015-10-20T09:41:55.767 回答
3

这很简单,使用此查询从两个日期之间的日期范围中查找选择数据

select * from tabblename WHERE (datecolumn BETWEEN '2018-04-01' AND '2018-04-5')
于 2018-06-05T09:04:00.813 回答
2

这适用于 SQL_Server_2008 R2

Select * 
from Product_sales
where From_date 
between '2013-01-03' and '2013-01-09'
于 2014-04-08T07:37:02.527 回答
2

您也可以尝试使用以下片段:

select  * from  Product_sales 
where  From_date  >= '2013-01-03' and game_date  <= '2013-01-09'
于 2017-08-08T21:08:34.047 回答
2
SELECT NULL  
    FROM   HRMTable hm(NOLOCK)  
    WHERE  hm.EmployeeID = 123
        AND (  
                (  
                    CAST(@Fromdate AS date) BETWEEN CAST(hm.FromDate AS date)  
                        AND CAST(hm.ToDate AS date)  
                )  
                OR (  
                    CAST(@Todate AS date) BETWEEN CAST(hm.FromDate AS date)  
                        AND CAST(hm.ToDate AS date)  
                   )  
                ) 
         )
于 2015-09-09T12:10:46.907 回答
1

这是一个查询,用于查找在 8 月份运行的所有产品销售

  • 查找 Product_sales 在 8 月份活跃
  • 包括在 8 月底之前开始的所有内容
  • 排除在 8 月 1 日之前结束的任何内容

还添加了一个 case 语句来验证查询

SELECT start_date, 
       end_date, 
       CASE 
         WHEN start_date <= '2015-08-31' THEN 'true' 
         ELSE 'false' 
       END AS started_before_end_of_month, 
       CASE 
         WHEN NOT end_date <= '2015-08-01' THEN 'true' 
         ELSE 'false' 
       END AS did_not_end_before_begining_of_month 
FROM   product_sales 
WHERE  start_date <= '2015-08-31' 
       AND end_date >= '2015-08-01' 
ORDER  BY start_date; 
于 2015-08-26T16:54:37.407 回答
1
DECLARE @monthfrom int=null,
@yearfrom int=null,
@monthto int=null,
@yearto int=null,
@firstdate DATE=null,
@lastdate DATE=null

SELECT @firstdate=DATEADD(month,@monthfrom-1,DATEADD(year,@yearfrom-1900,0)) /*Setting First Date using From Month & Year*/
SELECT @lastdate= DATEADD(day,-1,DATEADD(month,@monthto,DATEADD(year,@yearto-1900,0)))/*Setting Last Date using From Month & Year*/

SELECT *  FROM tbl_Record
WHERE  (DATEADD(yy, Year - 1900, DATEADD(m, Month - 1, 1 - 1)) BETWEEN CONVERT(DATETIME, @firstdate, 102) AND 
CONVERT(DATETIME, @lastdate, 102))
于 2019-07-26T12:54:51.563 回答
-1

您应该像比较数字值一样比较 sql 中的日期,

SELECT * FROM Product_sales
WHERE From_date >= '2013-01-01' AND To_date <= '2013-01-20'
于 2013-01-08T06:00:11.290 回答
-1

这很容易,使用这个查询来找到你想要的。

select * from Product_Sales where From_date<='2018-04-11' and To_date>='2018-04-11'
于 2018-05-06T08:55:18.717 回答