我正在尝试将小时和小时与双重保存在我的表格中保存的实际数据中。
TO_CHAR(sysdate,'HH.MI')返回char但我的数据包含浮点数
但它没有找到数据。
我正在尝试这样做:
create or replace function getSysTime
return char
is
hhhh intervals.interval_end%type;
v_interval_start intervals.interval_start%type;
v_interval_end intervals.interval_end%type;
v_interval_id intervals.interval_id%type;
begin
select INTERVAL_START ,INTERVAL_END , INTERVAL_ID , TO_CHAR(sysdate,'HH.MI')
into v_interval_start , v_interval_end , v_interval_id , hhhh
from INTERVALS
where hhhh = INTERVAL_START ;
return v_interval_id;
end;
解决了:
sloved by using cast char to float .
where cast (TO_CHAR(sysdate,'HH.MI') as float) between INTERVAL_START and INTERVAL_END ;
答案 0 :(得分:2)
欢迎使用Stack Overflow!
您的WHERE
子句通常用于将SELECT
语句限制为满足WHERE条件的行(即WHERE myTable.favoriteNumber = 5
)。或者,你可以在那里有一个布尔表达式。 WHERE 1=1
评估为WHERE TRUE
。因为它是TRUE
,所以返回所有行。 WHERE 0=2
对WHERE FALSE
进行评估,因此不会返回任何行,因为0在任何行中都不等于2。
无论如何,从逻辑上考虑一下。为了获得一组行,您需要为其提供参数。数据库如何知道您想要哪些行?首先,您必须使用SELECT
选择字段。哪张桌子?定义FROM
。您想要符合特定条件的行子集吗?添加WHERE
。我在哪里可以存储行中的值?添加INTO
。仅仅因为PL / SQL是程序性的并不意味着你总是从上到下,从左到右阅读。
在知道哪些行符合hhhh
条件之前,您的代码可能无法在WHERE
中插入值。因此,您有WHERE null = INTERVAL_START
。
如果此答案有助于回答您的问题,请选择左侧的“接受的答案”复选标记。
答案 1 :(得分:0)
看起来你正在寻找这样的东西?
-- 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