码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2020-01-16 10:29:36      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:项目   写法   使用   to_date   Plan   planning   介绍   time   时间段   

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

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

标签:项目   写法   使用   to_date   Plan   planning   介绍   time   时间段   

原文地址:https://www.cnblogs.com/hq233/p/12199676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!