2013-08-29 130 views
-1

我想比较小时和分钟从双重与实际数据保存在我的表中。比较to_char与浮点数

的TO_CHAR(SYSDATE,“HH.MI”)返回字符,但我的数据包含浮点数

但它说,没有发现任何数据。

我试图做到这一点:

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

你在表INTERVALS里面有一列'hhhh'吗? 'INTERVAL_START,INTERVAL_END,INTERVAL_ID,TO_CHAR(sysdate,'HH.MI')的结果是什么? – BLaZuRE

+0

hhhh不是列,它的变量有float类型,TO_CHAR返回char,所以当比较char和float时,它不会返回找到的数据。 –

+1

是的,它会引发“找不到数据”,因为您尚未在您的哪个部分为“hhhh”设置值。 – ajmalmhd04

回答

2

欢迎堆栈溢出!

您的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是程序化的并不意味着你总是从上到下地阅读,从左到右。

在知道哪些行满足您的WHERE条件之前,您的代码无法将值插入hhhh。因此,您有WHERE null = INTERVAL_START

如果此答案已帮助回答您的问题,请选择左侧的接受答案复选标记。

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