减去oracle中返回奇怪数据的时间戳

You*_* Qi 4 oracle timestamp subtraction

我试图减去两个日期并期望一些浮动值返回.但我得到的回报如下:

+000000000 00:00:07.225000
Run Code Online (Sandbox Code Playgroud)

将值乘以86400(我希望得到第二个差异)是获得更奇怪的值返回:

+000000007 05:24:00.000000000
Run Code Online (Sandbox Code Playgroud)

任何的想法?我怀疑是否与类型铸造有关.

a_h*_*ame 7

我猜你的列被定义为timestamp而不是date.

减去时间戳interval的结果date是a ,而减去列的结果是表示两个日期之间的天数的数字.

这在手册中有记录:http:
//docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements001.htm#i48042

因此,当您将时间戳列转换为日期时,您应该得到您期望的内容:

with dates as (
   select timestamp '2012-04-27 09:00:00' as col1,
          timestamp '2012-04-26 17:35:00' as col2
   from dual
)
select col1 - col2 as ts_difference,
       cast(col1 as date) - cast(col2 as date) as dt_difference
from dates;
Run Code Online (Sandbox Code Playgroud)

编辑:

如果你想转换间隔,例如秒数(作为数字),你可以这样做:

with dates as (
   select timestamp '2012-04-27 09:00:00.1234' as col1,
          timestamp '2012-04-26 17:35:00.5432' as col2
   from dual
)
select col1 - col2 as ts_difference,
       extract(hour from (col1 - col2)) * 3600 +  
       extract(minute from (col1 - col2)) * 60 + 
       (extract(second from (col1 - col2)) * 1000) / 1000 as seconds
from dates;
Run Code Online (Sandbox Code Playgroud)

以上的结果是 55499.5802