SQL> select * from tb11;ONE                TWO        THREE
------------------ ---------- ----------
aa                 bb         cc
dd                 ee         ff我想用個函數來抓表其中幾個字段,具體是那幾個要看條件,類似select fun(date) from 
(select * from tb11);
如果是8點就顯示one two
9點就顯示two three 
實際的情況字段更多 麻煩大家教下fun要怎么寫呢
本人是新人 請大家多多指教

解决方案 »

  1.   

    是当前时间?
    select decode(to_char(sysdate,'hh24'),'08',one,'09', two),
      decode(to_char(sysdate,'hh24'),'08',two,'09', three)
    from tb11
      

  2.   

    date是當前時間
    實際的字段不只有3個   是 8,9,10....17 
    這些字段都已經存在了
    想通過一個函數篩選出其中3個字段
    select fun(date) from 
    (select 8,9,10....17 from tb11); 
    根據時間顯示當前的小時已經前面兩個小時
    比如 現在是  11點  就顯示  9,10,11這3個字段
    15點就顯示 13,14,15這3個字段
      

  3.   

    create or replace function sel_col_by_date (
        i_date  in  timestamp
    ) return sys_refcursor
    as
        x_sql varchar2(200);
        o_cur  sys_refcursor;
    begin
        --如果是当前时间,可以去掉i_date参数,if里面用 to_char(sysdate,'HH24')
        if to_char(i_date,'HH24')='08' then
            x_sql :='select one,two from tb11';
        elsif to_char(i_date,'HH24')='09' then
    x_sql :='select two,three from tb11';
        elsif to_char(i_date,'HH24')='09' then  --其他的再加elsif
    x_sql :='select three,four from tb11';
        else
            x_sql :='select four,five from tb11';
        end if;
        open o_cur for x_sql;
        return o_cur;
    end sel_col_by_date;
    /--使用:select sel_col_by_date(sysdate) from dual;就可以了。
      

  4.   

    条件很多,则需要在程序中动态拼接SQL语句