它看起來像你正在尋找這樣的東西?
-- I'm lazy and provide only a few intervals
create table intervals as
select 1 as id, 8 + 0/60 as start_, 8 + 19/60 as end_, '8:00 - 8:19' as desc_ from dual union all
select 2 as id, 8 + 20/60 as start_, 8 + 39/60 as end_, '8:20 - 8:39' as desc_ from dual union all
select 3 as id, 8 + 40/60 as start_, 8 + 59/60 as end_, '8:40 - 8:59' as desc_ from dual union all
select 4 as id, 9 + 0/60 as start_, 9 + 19/60 as end_, '9:00 - 9:19' as desc_ from dual union all
select 5 as id, 9 + 20/60 as start_, 9 + 39/60 as end_, '9:20 - 9:39' as desc_ from dual union all
select 6 as id, 9 + 40/60 as start_, 9 + 59/60 as end_, '9:40 - 9:59' as desc_ from dual
;
實施例的查詢:
select * from intervals
where extract(hour from localtimestamp) +
(extract(minute from localtimestamp)/60)
between start_ and end_;
select * from intervals
where extract(hour from to_timestamp('2013-08-29 08:39:59', 'YYYY-MM-DD HH24:MI:SS')) +
(extract(minute from to_timestamp('2013-08-29 08:39:59', 'YYYY-MM-DD HH24:MI:SS'))/60)
between start_ and end_;
select * from intervals
where extract(hour from to_timestamp('2013-08-29 08:40:00', 'YYYY-MM-DD HH24:MI:SS')) +
(extract(minute from to_timestamp('2013-08-29 08:40:00', 'YYYY-MM-DD HH24:MI:SS'))/60)
between start_ and end_;
的便捷功能訪問間隔表:
create or replace function get_interval_id(
p_time in timestamp default localtimestamp
) return number as
v_id number;
begin
select id into v_id
from intervals
where extract(hour from p_time) +
(extract(minute from p_time)/60)
between start_ and end_;
return v_id;
exception
when others then
return null;
end;
/
show errors
如何使用功能:
SQL> select localtimestamp from dual;
LOCALTIMESTAMP
---------------------------------------------------------------------------
2013-08-29 09:41:51.388
SQL> select * from intervals where id = get_interval_id;
ID START_ END_ DESC_
---------- ---------- ---------- -----------
6 9.66666667 9.98333333 9:40 - 9:59
SQL> select * from intervals where id = get_interval_id(to_timestamp('2013-08-29 08:59:00', 'YYYY-MM-DD HH24:MI:SS'));
ID START_ END_ DESC_
---------- ---------- ---------- -----------
3 8.66666667 8.98333333 8:40 - 8:59
你在表INTERVALS裏面有一列'hhhh'嗎? 'INTERVAL_START,INTERVAL_END,INTERVAL_ID,TO_CHAR(sysdate,'HH.MI')的結果是什麼? – BLaZuRE
hhhh不是列,它的變量有float類型,TO_CHAR返回char,所以當比較char和float時,它不會返回找到的數據。 –
是的,它會引發「找不到數據」,因爲您尚未在您的哪個部分爲「hhhh」設置值。 – ajmalmhd04