简体   繁体   中英

SQL : find data by range of date

I have 2 records with field "from_date" and "to_date" :

 - Record 1 : from_date=2017-05-15 and to_date=2017-06-12
 - Record 2 : from_date=2018-03-20 and to_date=2018-04-11

how to get Record 2 if search from date :

 - 2018-03-01 and 2018-03-31?

or

 - 2018-04-01 and 2018-04-30?

or

 - 2018-04-01 and 2018-04-03?

This is as simple as :

SELECT * 
   FROM records 
    WHERE from_date >= @from_date 
         OR to_date <= @to_date 

Where @from_date and @to_date are just your variables.

I used an OR since you are not looking for a inclusive range. It is just a partial match.

You want a condition that match this record from_date=2018-03-20 and to_date=2018-04-11

Let's review the condition based on WHERE from_date >= @from_date OR to_date <= @to_date

@from_date = 2018-03-01 --false
@to_date   = 2018-03-31 --true
--
@from_date = 2018-04-01 --true
@to_date   = 2018-04-30 --false
--
@from_date = 2018-04-01 --true
@to_date   = 2018-04-03 --true

This shows that you only need one of the bounderies to match.

Note that based on the DBMS, the date comparison can vary, but the logic remain the same.

Try this sample:

Declare @date1 Date
Declare @date2 Date

set @date1 = <<give your first date>> 'yyyy-dd-mm
set @date2 = <<give your second date>> 'yyyy-dd-mm

SELECT * FROM tbldate WHERE CONVERT(DATE,@date1) BETWEEN from_date and to_date OR CONVERT(DATE,@date2) BETWEEN from_date and to_date

oracle example:

with 
    table1 as 
      ( select 1 id, to_date('2017-05-15','YYYY-MM-DD') date_from, to_date('2017-06-12','YYYY-MM-DD') date_to  from dual union all
        select 2 id, to_date('2018-03-20','YYYY-MM-DD') date_from, to_date('2018-04-11','YYYY-MM-DD') date_to  from dual )
select
    *
from 
    table1
where
/*
        to_date('2018-03-01','YYYY-MM-DD') < date_to
    and to_date('2018-03-31','YYYY-MM-DD') > date_from

        to_date('2018-04-01','YYYY-MM-DD') < date_to
    and to_date('2018-04-30','YYYY-MM-DD') > date_from
*/
        to_date('2018-04-01','YYYY-MM-DD') < date_to
    and to_date('2018-04-03','YYYY-MM-DD') > date_from
;

Thank you all for your response. I have found the answer.

WHERE ((from_date BETWEEN {fromDate} AND {toDate} OR to_date BETWEEN {fromDate} AND {toDate} OR (from_date <= {fromDate} AND to_date >= {fromDate}))

if using CakePHP3 :

->where([
        'OR' => [
            function($exp) use($fromDate,$toDate){                            
                return $exp->between('from_date',$fromDate,$toDate,'date');
            },
            function($exp) use($fromDate,$toDate){                            
                return $exp->between('to_date',$fromDate,$toDate,'date');
            }, 
            'AND' => [
                'from_date <=' => $fromDate,
                'to_date >=' => $fromDate
            ]
        ],
        'from_date is not' => null
    ]);

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