温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Oracle 计算时间格式平均值的sql 语句

发布时间:2020-09-13 22:37:43 来源:脚本之家 阅读:419 作者:虎咆狼哮 栏目:数据库

Oracle 计算时间格式平均值的sql 语句

select 
to_char((to_date('2019-07-01', 'yyyy-mm-dd') + numtodsinterval(avg(begin_time_second), 'second')),'hh34:mi:ss') avg_begin_time,
to_char((to_date('2019-07-01', 'yyyy-mm-dd') + numtodsinterval(avg(end_time_second), 'second')),'hh34:mi:ss') avg_end_time
--2019-07-01 00:00:00 + numtodsinterval(平均数,'秒')转换为日期格式,然后再转换为 时间字符格式
from 
(
  select 
  --把上班时间换算为秒
  to_char(a.actontime, 'hh34') * 3600 +
  to_char(a.actontime, 'mi') * 60 +
  to_char(a.actontime, 'ss') as begin_time_second,
  --把下班时间换算为秒 
  to_char(a.actofftime, 'hh34') * 3600 +
  to_char(a.actofftime, 'mi') * 60 + 
  to_char(a.actofftime, 'ss') as end_time_second
  from empworkdate a
)

知识点扩展:oracle中获取年月日时分秒

我写sql的时间为2016年10月10日下午15:18完事,这个时间下面要做对比:

--获取年
select to_char(sysdate,'yyyy') from dual--2016
select to_char(sysdate,'YYYY') from dual--2016
--获取月
select to_char(sysdate,'mm') from dual--10
select to_char(sysdate,'MM') from dual--10
--获取日
select to_char(sysdate,'dd') from dual--10
select to_char(sysdate,'DD') from dual--10
--获取时
select to_char(sysdate,'hh34') from dual--15
select to_char(sysdate,'hh') from dual--03
select to_char(sysdate,'HH') from dual--03
select to_char(sysdate,'HH24') from dual--15
--获取分钟
select to_char(sysdate,'MI') from dual--14
select to_char(sysdate,'mi') from dual--14
--获取秒
select to_char(sysdate,'ss') from dual--35
select to_char(sysdate,'SS') from dual--40
--获取年月日时分秒
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual--2016-10-10 03:17:25
select to_char(sysdate,'yyyy-mm-dd hh34:mi:ss') from dual--2016-10-10 15:17:51

(1)从上面的测试SQL可以看出年月日yyyy,mm,dd,hh,mi,ss的大小写对于从oracle中获取年月日时分秒没有影响

(2)对于获取小时分12小时和24小时的情况,如果你想获取显示下午的时间,你可以采用hh34,这样就会显示类似于15:30而不是03:30

总结

以上所述是小编给大家介绍的Oracle 计算时间格式平均值的sql 语句,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI