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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值