码迷,mamicode.com
首页 > 数据库 > 详细

Oracle计算时间跨度的函数

时间:2015-06-09 17:45:14      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:oracle   时间跨度   

显示距离当前时间的跨度

-- 计算时间跨度:返回值为X天y时z分

declare

  ln_day_count      number(10);

  ln_hour_count     number(10);

  ln_min_count      number(10);

  ls_time_sent_last varchar2(20) := ‘2015-06-07 08:40:20‘;

  ls_timespan varchar2(100) := ‘‘;

begin 

  -- 计算天数

  select floor(sysdate - To_date(ls_time_sent_last, ‘yyyy-mm-dd hh24-mi-ss‘))  into ln_day_count FROM dual;

  if ln_day_count != 0 then

    ls_timespan := to_char(ln_day_count) || ‘天‘;

    dbms_output.put_line(ls_timespan);

  end if;

  -- 计算小时数

  select mod(floor(((sysdate - To_date(ls_time_sent_last, ‘yyyy-mm-dd hh24-mi-ss‘))) * 24),24) into ln_hour_count FROM dual;

  if ln_hour_count != 0 then

    ls_timespan := ls_timespan || to_char(ln_hour_count) || ‘时‘;

    dbms_output.put_line(ls_timespan);

  end if;

  -- 计算分钟数

  select mod(mod(floor(((sysdate - To_date(ls_time_sent_last, ‘yyyy-mm-dd hh24-mi-ss‘))) * 24 * 60),24*60),60) into ln_min_count FROM dual;

  if ln_min_count != 0 then

    ls_timespan := ls_timespan || to_char(ln_min_count) || ‘分‘;

    dbms_output.put_line(ls_timespan);

  end if;

end; 



Oracle计算时间跨度的函数

标签:oracle   时间跨度   

原文地址:http://lifudong.blog.51cto.com/2357095/1660070

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!