您的位置:首页 > 数据库 > Oracle

秒转为时分秒的oracle函数

2016-06-12 16:14 453 查看
create or replace function GETTIMEDESC(seconds in integer) return  varchar2 is
Result varchar2(100);
begin
Result := '1t';

if (seconds<24*3600) then
select to_char(to_date(mod(seconds,3600*24),'sssss'),'fmhh24"小时"mi"分"ss"秒"') into Result from dual;
else
select trunc(seconds/3600/24) || '天' || to_char(to_date(mod(seconds,3600*24),'sssss'),'fmhh24"小时"mi"分"ss"秒"') into Result from dual;
end if;

return(Result);
end GETTIMEDESC;


这个函数没有太多的技术含量,记录下来。网上找了好久,后来也是看一个答案,稍加改造而来

调用sql语句 select  GETTIMEDESC(500000) m from dual; 

好像又回去了,写来写去,感觉很简单,但是网上就没有这个现成的答案,简单是事情我来做。

 

调试了好久,发现上面的写法是有问题的,换个写法,搞了好久,实在是不成了,只好整个笨办法,麻烦点,但肯定是对的。

create or replace function GETTIMEDESC(seconds in number) return  varchar2 is
Result varchar2(100);
--DaySeconds number;
TempSconds number;
begin
Result := '';

--DaySeconds := 24*3600;
TempSconds := seconds;

if (seconds is null) then
return '';
end if;

--天数
if (seconds>=86400) then
Result := round(seconds/86400) || '天';
TempSconds := mod(seconds,86400);
else
Result := '0天';
end if;

--小时
if (TempSconds>=3600) then
Result := Result || round(TempSconds/3600) || '小时';
TempSconds := mod(TempSconds,3600);
else
Result := Result || '0小时';
end if;

--分钟
if (TempSconds>=60) then
Result := Result || round(TempSconds/60) || '分钟';
TempSconds := mod(TempSconds,60);
else
Result := Result || '0分钟';
end if;

Result := Result || to_char(round(TempSconds)) || '秒';

return(Result);

end GETTIMEDESC;

 

对自己来说是个记录,希望对需要的人能或多或少地帮助一点点

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: