简体   繁体   中英

BETWEEN @Start and @End Date; Not pulling in records which fall beyond the date

Currently working on a t-sql query which is supposed to get a list of all records that fall between the current fiscal year (we use 4-4-5 calendar). The record's start and end date are 06/02/2018, 31/07/2020.

The date I am filtering for is:

DECLARE @StartDate DATE = '12-29-2018';
DECLARE @EndDate DATE = '12-31-2019'; 

The condition I have in my where clause is:

 AND
          (

          (o.Revenue_Start_Date__c >= @StartDate AND  o.Revenue_End_Date__c <= @EndDate) OR (o.Revenue_End_Date__c >= @StartDate AND  o.Revenue_End_Date__c <= @EndDate)
          )

I have tried variations of BETWEEN as well. Any idea what I may be doing wrong and how do I get a list of all records to be included if it falls within the coresponding dates.

ANSWER:

AND  (  -- Starts Within range
  ( o.Revenue_Start_Date__c 
    BETWEEN @StartDate
    AND     @EndDate
  )
OR  -- Ends within range
  (
    o.Revenue_End_Date__c
    BETWEEN @StartDate
    AND     @EndDate
   )
OR    -- SPANS Range
   (
     o.Revenue_Start_Date__c < @StartDate
     AND
     o.Revenue_End_Date__c > @EndDate  
    )
    )

This seems to be working for me just now.

For fall through cases, this should work.

o.Revenue_Start_Date__c <= @EndDate AND o.Revenue_End_Date__c >= @StartDate

在此处输入图片说明

Is this what you want?

(o.Revenue_Start_Date__c < @EndDate AND 
 o.Revenue_End_Date__c >= @StartDate
)

This is the logic for overlapping intervals. Note that the inequalities might be exact or inexact depending on your actual definitions of overlapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM