20181228-Oracle_SQL部分_时间转换(案例一)

在这里插入图片描述

问题:

同事反馈XXX 系统里记录的耗时为字符类型,通过execl 不方便计算各个耗时统计,咨询我是否有好的方法;

思路:

数据带有天、小时、分,字符类型无法直接计算,可以通过oracle 数据库将天、小时、分钟数据单独拆分出来,即把天和小时都转换成分钟,最终耗时字段只有分钟,方便后期运算;

步骤如下:

1 将exec 数据导入到Oracle 数据库里;
2 查询数据

select 流程最长耗时 from 流程监控 ;

在这里插入图片描述
3 通过substr 截取字符串,再加上instr 获取位置,可以实现将天、小时、分拆分出来;

— 天

select 流程最长耗时 , nvl ( substr ( 流程最长耗时 , 1 , instr ( 流程最长耗时 , ' 天' , - 1 ) - 1 ),0 )天 from 流程监控 ;

— 小时

select 
流程最长耗时 ,
substr ( 流程最长耗时 , instr ( 流程最长耗时 , ' 天' ) + 1 , instr ( 流程最长耗时 , ' 小时' ) - instr ( 流程最长耗时 , ' 天' ) - 1 ) 小时 
from 
流程监控 ;

— 分钟

select 
流程最长耗时 ,
substr ( 流程最长耗时 , instr ( 流程最长耗时 , ' 小时' ) + 2 , instr ( 流程最长耗时 , ' 分' ) - instr ( 流程最长耗时 , ' 小时' ) - 2 ) 分钟 
from
流程监控 ;

4 将天、小时转换成分钟,并求出总分钟数

select
流程最长耗时 ,
nvl ( substr ( 流程最长耗时 , 1 , instr ( 流程最长耗时 , '天' , - 1 ) - 1 ), ) * 1440 + substr ( 流程最长耗时 , instr ( 流程最长耗时 , '天' ) + 1 , instr ( 流程最长耗时 , '小时' ) - instr ( 流程最长耗时 , '天' ) - 1 )* 60 + substr ( 流程最长耗时 , instr ( 流程最长耗时 , '小时' ) + 2 , instr ( 流程最长耗时 , '分' ) - instr ( 流程最长耗时 , '小时' ) - 2 )
  from
流程监控 ;

在这里插入图片描述

欢迎关注我的微信公众号"IT小Chen"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值