Oracle 与带有float的_char进行比较
我试图将dual中的小时和分钟与表中保存的实际数据进行比较 TO_CHARsysdate,'HH.MI'返回char,但我的数据包含浮点数 但是它说没有发现任何数据 我正在尝试这样做:Oracle 与带有float的_char进行比较,oracle,plsql,Oracle,Plsql,我试图将dual中的小时和分钟与表中保存的实际数据进行比较 TO_CHARsysdate,'HH.MI'返回char,但我的数据包含浮点数 但是它说没有发现任何数据 我正在尝试这样做: create or replace function getSysTime return char is hhhh intervals.interval_end%type; v_interval_start intervals.interval_st
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 ;
欢迎来到堆栈溢出 WHERE子句通常用于将SELECT语句限制为满足WHERE条件的行,即WHERE myTable.favoriteNumber=5。或者,您可以在其中使用布尔表达式。其中1=1的计算结果为TRUE。因为这是真的,所以返回所有行。其中0=2求值为FALSE,因此不返回任何行,因为任何行中0都不等于2 无论如何,要有逻辑地思考。为了获得一组行,需要为其提供参数。数据库如何知道需要哪些行?首先,必须使用select选择字段。哪张桌子?从定义。是否需要满足特定条件的行子集?添加一个WHERE。在哪里可以存储行中的值?添加一个到。仅仅因为PL/SQL是过程性的,并不意味着您总是从上到下、从左到右阅读 在知道哪些行符合WHERE条件之前,代码不可能在hhhh中插入值。因此,这里有null=INTERVAL\u START
如果此答案有助于回答您的问题,请选择左侧的“已接受答案”复选标记。看起来您正在寻找类似的答案
-- 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
;
示例查询:
访问间隔表的便捷功能:
如何使用该功能:
在表格间隔内有hhhh列吗?从HHH=INTERVAL\u START;的INTERVAL中选择INTERVAL\u START、INTERVAL\u END、INTERVAL\u ID到\u CHARsysdate、'HH.MI'的结果是什么;?hhhh不是列,它的变量的类型为float,而TO_CHAR返回CHAR,因此当比较CHAR和float时,它返回未找到的数据。是的,它将不返回未找到的数据,因为您尚未在where部分中为hhhh设置值。这没有多大意义。MI可能看起来像一个十进制数字,但它不是。MI的计算结果介于00和59之间,因此,例如,0.5表示50分钟,而不是半小时。
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