首先,來介紹一下Oracle的overlaps函數的用法:
overlaps函數是用來判斷兩個時間段是否有重疊的
比如說計算 (a,b),(c,d)
就可以寫成:
select * from dual where (a,b) overlaps (c,d);
其中abcd都是日期格式。
注意:比較的只能是日期。如果是數字,則會報錯 ORA-00932: inconsistent datatypes
最近項目在使用db2數據庫,遇到overlaps函數需要轉換成db2中的語法方言,其實就是轉換為通用的sql語法。
對應的寫法有:
判斷兩個時間段是否有重疊
(a,b),(c,d)
判斷兩段時間是否有重疊
方法一
select 'yes' from dual where d>a and c<b;
例子:
select *
from PMK_PLANNING_INFO
where id = 'U2KsqII5NdumrClO5td'
and to_date('2019/07/27', 'YYYY/MM/DD') > to_date(begin_time)
and to_date('2018/06/27', 'YYYY/MM/DD') < to_date(end_time);
方法二
select 'yes' from dual where (a, b) overlaps (c,d);
例子:
select *
from PLANNING_INFO
where id = '001'
and (to_date(begin_time), to_date(end_time))
overlaps
(to_date('2018/06/27', 'YYYY/MM/DD'),
to_date('2019/07/27', 'YYYY/MM/DD'));
方法三
select 'yes' from dual where a between c and d or d between a and b;
例子:
select *
from PMK_PLANNING_INFO
where id = 'U2KsqII5NdumrClO5td'
and to_date(begin_time) between to_date('2018/06/27', 'YYYY/MM/DD') and to_date('2019/07/27', 'YYYY/MM/DD')
or to_date('2019/07/27', 'YYYY/MM/DD') between to_date(begin_time) and to_date(end_time);
a : to_date(begin_time)
b :to_date(end_time)
c :to_date('2018/06/27', 'YYYY/MM/DD')
d :to_date('2019/07/27', 'YYYY/MM/DD')
