ORACLE SQL 小时范围

ORACLE SQL Range of hours

我有一个问题,我有一个有 3 列的 table

- date - varchar2   - varchar2 -   
| date | start_hour | end_hour |  

我需要进行验证以确保时间范围不重叠。
例如:

| date  | start_hour | end_hour |  
| date1 | 09:00AM    | 09:30AM  |  
| date1 | 10:30AM    | 11:30AM  |  
| date1 | 01:00PM    | 03:00PM  |  

假设 3 行的日期相同。
我需要的是不重叠这些范围
我无法插入这样的范围
start_hour = 08:00AM 和 end_hour = 09:20AM,因为 09:00AM 和 09:30AM 之间的范围已经存在,所以,新范围与table 中存在的范围。 我尝试了很多查询,没有介于两者之间,我插入的 end_hour 需要小于 table 中的 start_hour。 有人知道怎么做吗?

我假设您已经将时间格式转换为 hh24:mi

也许这会有所帮助:

with tab as(
select 'date1' as dat,  '09:00' as  start_hour, '09:30' as end_hour from dual union all
select 'date1' as dat,  '10:30' as  start_hour, '11:30' as end_hour from dual union all
select 'date1' as dat,  '13:00' as  start_hour, '15:00' as end_hour from dual 
)
SELECT COUNT(*)
  FROM   tab
  WHERE  start_hour <= '09:10' --:new_end_hour
  AND    end_hour   >= '07:00' --:new_start_hour
  AND    dat = 'date1'
  ;

或者您可以使用 between 来检查它 start_hourend_hour 在值

之间
with tab as(
select 'date1' as dat,  '09:00' as  start_hour, '09:30' as end_hour from dual union all
select 'date1' as dat,  '10:30' as  start_hour, '11:30' as end_hour from dual union all
select 'date1' as dat,  '13:00' as  start_hour, '15:00' as end_hour from dual 
)
SELECT COUNT(*)
  FROM   tab
  WHERE  ('09:00' between start_hour and end_hour
  or    '09:10' between start_hour and end_hour
  )
  AND    dat = 'date1'
  ;

db<>fiddle here

我已经找到了我的问题的解决方案,作为我问题中评论的推荐,当我更改小时格式和新小时格式时,它工作得很好。 这是将来遇到同样问题的人的代码。

DECLARE
  l_count NUMBER(1) := 0;
BEGIN

  SELECT COUNT(*)
  INTO   l_count
  FROM   table
  WHERE  start_hour <= :new_start_hour
  AND    end_hour   >= :new_end_hour
  AND    date = :date
  AND    ROWNUM     = 1;
  dbms_output.put_line(l_count);

END;
/

感谢大家的帮助。