如何确定两列之间的时差/持续时间,其格式如下: HHMMSS。
前导零被系统(Oracle DB)吞没。 0点是“ 0”。 3点是“ 300”。 23:50的时钟是“ 235000”。
无论如何,24:00也被报告为“ 240000”。 这意味着我有“开始”列和“结束”列。
如何使用SQL确定持续时间?
答案 0 :(得分:2)
使用算术将值转换为秒:
select hhmmss, floor(hhmmss / 10000) * 60 * 60 + (mod(hhmmss, 10000) / 100) * 60 + mod(hhmmss, 100)
from (select 235000 as hhmmss from dual) x
然后减去秒。这将为您提供持续时间(以秒为单位)。
使用您的列名,看起来像这样:
select ( (floor(end / 10000) * 60 * 60 + (mod(end, 10000) / 100) * 60 + mod(end, 100)) -
(floor(start / 10000) * 60 * 60 + (mod(start, 10000) / 100) * 60 + mod(start, 100))
) as diff_seconds
要将其转换回字符串,可以使用to_char()
:
select to_char(date '2000-01-01' + diff_seconds * interval '1' second, 'HH24:MI:SS')
答案 1 :(得分:0)
您可以使用to_date
来达到以小时/分钟/秒为单位的持续时间,如下所示:
Select round(
(to_date(lpad(end,6,'0'),'HHMISS')
- to_date(lpad(start,6,'0'),'HHMISS') ) * 24
, 2) diff_in_hours
-- multiply it by another 60 to get diff in minutes (24*60)
-- multiply it by another 3600 to get diff in seconds (24*3600)
from your_table
要在HHMISS中获得输出,只需将其添加到sysdate
Select
TO_CHAR(Trunc(sysdate) + (to_date(lpad(end,6,'0'),'HHMISS')
- to_date(lpad(start,6,'0'),'HHMISS')) , 'HH:MI:SS') as diff_HHMISS
from your_table
干杯!