将TIMESTAMP差异列数据转换为字符串格式

时间:2014-05-07 07:32:43

标签: sql oracle

从我的观点来看,我得到一个值为'0 0:0:0.343009'的列,它显示了两个时间戳之间的差异,精确到毫秒级。

我想将它们显示为343毫秒或其他值的字符串,但应该是毫秒转换。

我有similar post in the past但该时间列是DATE数据类型,这次它是TIMESTAMP。我正在使用FLOOR函数将输出更改为数值,以显示更加用户友好的结果。

我使用示例查询来查找日期的差异。这里created_timeTIMESTAMP数据类型:

select msg_guid,
   (max(case when payload_type = 1 then created_time end) -
    (case when max(case when payload_type = 2 then created_time end) <>
               trunc(max(case when payload_type = 2 then created_time end))
          then max(case when payload_type = 2 then created_time end) 
          when max(case when payload_type = 3 then created_time end) <>
               trunc(max(case when payload_type = 3 then created_time end))
          then max(case when payload_type = 3 then created_time end) 
    end)
    ) as diff              
from table t
 group by msg_guid;

1 个答案:

答案 0 :(得分:2)

添加或减去时间戳the result is an interval时,而不是其他时间戳。您可以使用the extract function从中提取组件。如果你的值总是低于秒,你可以只提取秒,然后乘以一千来得到毫秒:

with t as (
  select 1 as msg_guid,
    interval '0 0:0:0.343009' day to second as diff
  from dual
)
select trunc(extract (second from diff) * 1000)
from t;

TRUNC(EXTRACT(SECONDFROMDIFF)*1000)
-----------------------------------
                                343 

在这里,您的真实查询将取代我用的间隔文字的虚拟CTE。

如果间隔时间可能超过一秒,那么您可能希望以毫秒为单位获取整个值,因此您需要提取所有元素并将它们相加,并根据它们代表的内容进行相乘 - 所以整整一天将是86400000毫秒等;普通元素会像:

column diff format a25
with t as (
  select 1 as msg_guid,
    systimestamp - trunc(systimestamp) as diff
  from dual
)
select diff,
  extract (day from diff) as dd,
  extract (hour from diff) as hh,
  extract (minute from diff) as mi,
  extract (second from diff) as ss
from t;

DIFF                           DD         HH         MI         SS
---------------------- ---------- ---------- ---------- ----------
0 9:13:26.150627                0          9         13  26.150627 

你可以将它们组合起来:

with t as (
  select 1 as msg_guid,
    systimestamp - trunc(systimestamp) as diff
  from dual
)
select diff,
  trunc(1000 * (
    extract (day from diff) * (60*60*24)
    + extract (hour from diff) * (60*60)
    + extract (minute from diff) * 60
    + extract (second from diff)
  )) as milliseconds
from t;

DIFF                   MILLISECONDS
---------------------- ------------
0 9:13:27.650365           33207650 

但根据您之前的问题,也许您希望将其作为字符串,作为单独的组件:

with t as (
  select 1 as msg_guid,
    systimestamp - trunc(systimestamp) as diff
  from dual
)
select diff,
  extract (day from diff) || ' DAYS '
    || extract (hour from diff) || ' HOURS '
    || extract (minute from diff) || ' MINUTES '
    || trunc(extract (second from diff)) || ' SECONDS '
    || (trunc(extract (second from diff) * 1000)
      - (trunc(extract (second from diff)) * 1000)) || ' MILLISECONDS'
    as text
from t;

DIFF                   TEXT                                                   
---------------------- -------------------------------------------------------
0 9:43:38.896007       0 DAYS 9 HOURS 43 MINUTES 38 SECONDS 896 MILLISECONDS

SQL Fiddle根据您的样本数据,排序,并且时间计算反转,因此值为正。