日期:2014-05-17  浏览次数:20865 次

在Oracle中的时间条件怎么写啊?
我象实现的功能是:
如果现在的时间是8:00到16:00间
执行语句select   to_date(sysdate, 'yyyy ')   from   dual
如果现在的时间是16:00到00:00间
执行语句select   to_date(sysdate, 'yyyy-mm ')   from   dual
如果现在的时间是00:00到08:00间
执行语句select   to_date(sysdate, 'yyyy-mm-dd ')   from   dual
我写的这句老是有错误,也不知道错在哪里。
if   to_char(sysdate, 'hh24 ')   > =08   and   to_char(sysdate, 'hh24 ') <16   then
      select   to_date(sysdate, 'yyyy ')   from   dual;
elsif   to_char(sysdate, 'hh24 ')   > =16   and   to_char(sysdate, 'hh24 ') <00   then
      select   to_date(sysdate, 'yyyy-mm ')   from   dual;
elsif   to_char(sysdate, 'hh24 ')   > =00   and   to_char(sysdate, 'hh24 ') <08   then
      select   to_date(sysdate, 'yyyy-mm-dd ')   from   dual;
end   if;

------解决方案--------------------
select to_date(sysdate, 'yyyy ') from dual
select to_date(sysdate, 'yyyy-mm ') from dual
select to_date(sysdate, 'yyyy-mm-dd ') from dual
3句语句本身就是错误的


------解决方案--------------------
date型的还to-date就是错的

------解决方案--------------------
if to_number(to_char(sysdate, 'hh24 ')) > =8 and to_char(to_char(sysdate, 'hh24 ')) <16 then
select to_date(sysdate, 'yyyy ') from dual;
elsif to_number(to_char(sysdate, 'hh24 ')) > =16 and to_number(to_char(sysdate, 'hh24 ')) <0 then
select to_date(sysdate, 'yyyy-mm ') from dual;
elsif to_number(to_char(sysdate, 'hh24 ')) > =0 and to_number(to_char(sysdate, 'hh24 ')) <8 then
select to_date(sysdate, 'yyyy-mm-dd ') from dual;
end if;

------解决方案--------------------
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss ') from dual;
单独这样执行可以

declare aaa date;
begin
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss ') into aaa from dual;

end;