-1

我正在尝试将来自 dual 的 Hour 和 min 与保存在我的表中的实际数据进行比较。

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 ;
4

2 回答 2

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

hhhh在知道哪些行符合您的要求之前,您的代码不可能插入一个值WHERE在知道哪些行满足您的条件因此,您有WHERE null = INTERVAL_START.

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

于 2013-08-29T03:19:44.337 回答
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
于 2013-08-29T06:51:47.870 回答