从两个日期之间的日期范围中选择数据

时间:2013-01-08 05:51:00

标签: sql date range

我有一个名为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

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

例如,我想从2013-01-032013-01-09选择销售数据。

19 个答案:

答案 0 :(得分:106)

interval intersection description

正如您所看到的,有两种方法可以完成任务:

  • 登记所有可接受的选项
  • 排除所有错误选项

显然,第二种方式更简单(只有两种情况对抗四种情况)。

您的SQL将如下所示:

SELECT * FROM Product_sales 
WHERE NOT (From_date > @RangeTill OR To_date < @RangeFrom)

答案 1 :(得分:56)

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_Date To_Date 可能介于您的日期范围之间,或者记录日期可能涵盖整个范围。

如果某个From_dateTo_date位于日期之间,或From_date小于开始日期且To_date大于结束日期;然后应返回此行。

答案 2 :(得分:33)

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

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

答案 3 :(得分:15)

SELECT * FROM Product_sales 
WHERE From_date between '2013-01-03'
AND '2013-01-09'

答案 4 :(得分:4)

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'
)

答案 5 :(得分:3)

请尝试:

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)

答案 6 :(得分:3)

select * 
from table 
where
( (table.EndDate > '2013-01-05') and (table.StartDate < '2013-01-07' )  )

答案 7 :(得分:3)

此查询可以帮助您:

select * 
from XXXX
where datepart(YYYY,create_date)>=2013 
and DATEPART(YYYY,create_date)<=2014

答案 8 :(得分:2)

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

SELECT * from Product_sales where (From_date <= '2013-01-09' AND To_date >= '2013-01-01')

答案 9 :(得分:2)

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

select * from tabblename WHERE (datecolumn BETWEEN '2018-04-01' AND '2018-04-5')

答案 10 :(得分:2)

这适用于SQL_Server_2008 R2

Select * 
from Product_sales
where From_date 
between '2013-01-03' and '2013-01-09'

答案 11 :(得分:2)

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

因此:

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

它总是对我有用; - )

答案 12 :(得分:1)

检查此查询,我创建了此查询,以检查是否有任何预约日期的检查日期

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

这将重新显示重叠的细节,以获得不重叠的细节,然后删除“不”。来自查询

答案 13 :(得分:1)

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)  
                   )  
                ) 
         )

答案 14 :(得分:1)

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

select  * from  Product_sales 
where  From_date  >= '2013-01-03' and game_date  <= '2013-01-09'

答案 15 :(得分:0)

此代码效果很好:

控制器:

$this->load->model("YourModelName");
$data ['query'] = $this->YourModelName->get_report();

型号:

  public function get_report()
     {   
       $query = $this->db->query("SELECT  *
FROM   reservation WHERE arvdate <= '2016-7-20' AND  dptrdate >= '2016-10-25' ");
       return $query;
     }

其中&#39; arvdate&#39;和&#39; dptrdate&#39;是数据库中的两个日期和&#39;预订&#39;是表名。

查看:

<?php
 echo $query->num_rows();
?>

此代码用于返回行数。 要返回表数据,请使用

$query->rows();
return $row->table_column_name;

答案 16 :(得分:0)

这很简单,使用此查询查找您想要的内容。

select * from Product_Sales where From_date<='2018-04-11' and To_date>='2018-04-11'

答案 17 :(得分:0)

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))

答案 18 :(得分:0)

您应该比较数字值

来比较sql中的日期
SELECT * FROM Product_sales
WHERE From_date >= '2013-01-01' AND To_date <= '2013-01-20'