在Oracle中执行以下计算(end_time减去start_time)时,得到+00 01:30:00.000000的结果。如何将结果转换为1.50?
to_timestamp(to_char(end_time,'HH:MI AM'), 'HH:MI AM') -
to_timestamp(to_char(start_time,'HH:MI AM'), 'HH:MI AM')
答案 0 :(得分:1)
尝试这样的事情:
select dat,extract(hour from dat) ||'.'||trunc(100*extract(minute from dat)/60,0) result from
(select to_timestamp('04:30 AM', 'HH:MI AM') - to_timestamp('03:10 AM', 'HH:MI AM') as dat from dual union all
select to_timestamp('02:30 AM', 'HH:MI AM') - to_timestamp('02:00 AM', 'HH:MI AM') as dat from dual)
结果:
DAT RESULT
--------------------------------------------------------------------------------
+00 01:20:00.000000 1.33
+00 00:30:00.000000 0.50
答案 1 :(得分:1)
The difference between two TIMESTAMP
s is an INTERVAL
。您可以轻松提取各种组件,并通过一些算术获得所需的结果。在一般情况下(对于小数天的结果!):
with testdata as (
select NUMTODSINTERVAL(1.55,'DAY') i from dual
)
select EXTRACT(DAY FROM i)
+ EXTRACT(HOUR FROM i)/24
+ EXTRACT(MINUTE FROM i)/(24*60)
+ EXTRACT(SECOND FROM i)/(24*60*60)
FROM testdata
在您的特定情况下,假设(1)"时间戳是数据类型" (2)您"忽略当天,第二和毫秒组件" (3)您想要小数小时:
select ...
EXTRACT(HOUR FROM (end_time-start_time))
+ EXTRACT(MINUTE FROM (end_time-start_time))/(60)
FROM ...
答案 2 :(得分:0)
替代方法:由于您的时间只是精确到分钟,您也可以使用DATE,减法产生十进制数,例如:
to_date(to_char(end_time,'HH24:MI'), 'HH24:MI') -
to_date(to_char(start_time,'HH24:MI'), 'HH24:MI')
如果日期部分保证在同一日期,您可以使用更简单的表达式来获得相同的结果:
end_time - start_time