2013-08-29 165 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