在SqlServer 2008中比较两个日期和另外两个日期。

时间:2022-10-25 14:21:00

I have a table in sql server with following schema.

我在sql server中有一个具有以下模式的表。

   **ID   Title     ActiveFromDate  ActiveToDate**
    1      Test       2013-07-06      2013-08-22
    2      Test       2013-07-01      2013-07-30

I want to select the records by ActiveFromDate and Active ToDate, i.e. to select all those record which are between certain date Ranges.

我想通过ActiveFromDate和Active ToDate来选择记录,也就是说,要选择所有那些在特定日期范围内的记录。

     SELECT ISNULL(COUNT(*),0) AS Total, dbo.ArrangementPromotion.Title
FROM dbo.ArrangementPromotion
WHERE(
       (dbo.ArrangementPromotion.ActiveFromDate BETWEEN '2013-08-01' AND '2013-08-14') 
    OR (dbo.ArrangementPromotion.ActiveToDate BETWEEN '2013-08-01' AND '2013-08-14' )
     )
     AND dbo.ArrangementPromotion.Inactive = 0 
     AND dbo.ArrangementPromotion.Deleted = 0
     AND(12 IS NOT NULL OR dbo.ArrangementPromotion.ID <> 12 )
GROUP BY dbo.ArrangementPromotion.Title

But i am not getting the desire result if the dates in parameter @ActiveToDate is less than table Active ToDate. Can any body help me in this.

但是,如果参数@ActiveToDate中的日期小于表的活动日期,则不会得到期望结果。在这件事上有谁能帮助我吗?

Thanks

谢谢

1 个解决方案

#1


2  

I think you might be looking for something like

我想你可能在找类似的东西

   (dbo.ArrangementPromotion.ActiveFromDate BETWEEN '2013-08-01' AND '2013-08-14') 
OR (dbo.ArrangementPromotion.ActiveToDate BETWEEN '2013-08-01' AND '2013-08-14' )
OR ('2013-08-01' BETWEEN dbo.ArrangementPromotion.ActiveFromDate AND dbo.ArrangementPromotion.ActiveToDate)
OR ('2013-08-14' BETWEEN dbo.ArrangementPromotion.ActiveFromDate AND dbo.ArrangementPromotion.ActiveToDate)

Where '2013-08-01' should be @ActiveFrom and '2013-08-14' @ActiveTo

“2013-08-01”应该是@ActiveFrom还是“2013-08-14”@ acti否决权在哪里

#1


2  

I think you might be looking for something like

我想你可能在找类似的东西

   (dbo.ArrangementPromotion.ActiveFromDate BETWEEN '2013-08-01' AND '2013-08-14') 
OR (dbo.ArrangementPromotion.ActiveToDate BETWEEN '2013-08-01' AND '2013-08-14' )
OR ('2013-08-01' BETWEEN dbo.ArrangementPromotion.ActiveFromDate AND dbo.ArrangementPromotion.ActiveToDate)
OR ('2013-08-14' BETWEEN dbo.ArrangementPromotion.ActiveFromDate AND dbo.ArrangementPromotion.ActiveToDate)

Where '2013-08-01' should be @ActiveFrom and '2013-08-14' @ActiveTo

“2013-08-01”应该是@ActiveFrom还是“2013-08-14”@ acti否决权在哪里