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

标签 oracle timestamp subtraction

我正在尝试减去两个日期并期望返回一些 float 值。但我得到的返回如下:

+000000000 00:00:07.225000

将值乘以 86400(我想得到秒的差异)会得到一些更奇怪的返回值:
+000000007 05:24:00.000000000

任何的想法?我怀疑与类型转换有关。

最佳答案

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

减去时间戳的结果是 interval而减去date的结果columns 是一个数字,表示两个日期之间的天数。

这在手册中有记录:
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;

编辑 :

如果要转换间隔,例如秒数(作为数字),您可以执行以下操作:
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;

上面的结果是55499.5802

关于oracle - 在oracle中减去时间戳返回奇怪的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10348140/

相关文章:

php - 在 SQL 查询中使用日期时间边距

python - 使用 Pandas 查找具有 Null 的 2 列之间的差异

javafx - 如何在 JavaFX 中正确从路径中减去形状

oracle - weblogic.Server 创建域但不启动它( Chef Recipe )

java - 执行sql查询时出错

Python 将时间戳转换为日期

python - 使用 SQL 和 python 将时间单位添加到时间戳

Java模式正则表达式减法与贪婪量词

sql - 如果不存在插入失败,并出现错误 Unknown command for If Not Exists statements

java - 使用 Spring Data JPA 正确调用存储过程