Oracle的overlaps函数转换其他数据库语法

首先,来介绍一下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

例子:

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

你可能感兴趣的:(Oracle的overlaps函数转换其他数据库语法)