将数字转换为日期

时间:2015-02-25 08:27:41

标签: sql oracle type-conversion

我有一个计算,我从中减去两个日期值。我收到一个十进制值,乘以60(秒),60(分钟),24(小时)。 该值为我提供了特定ID在系统中花费的秒数。

但我希望在日期值中再次计算此计算结果,因此我可以在设计程序中使用日期值。

select to_date((t2.time_event - t1.time_event) * 24 * 60 * 60,'hh24:mm:ss') as "Throughput_Time", count(t1.) as "Payments"
    from TBL_DUMMYFEED t1 
    join TBL_DUMMYFEED t2 on t1.trax_id = t2.trax_id
    where t1.event = 'created' and t2.event = 'sent'
    group by to_date((t2.time_event - t1.time_event) * 24 * 60 * 60,'hh24:mm:ss')
    order by to_date((t2.time_event - t1.time_event) * 24 * 60 * 60,'hh24:mm:ss');

我被困了,因为我无法把事情搞定。

1 个答案:

答案 0 :(得分:1)

看起来非常笨拙,但如果您真的需要转换为日期数据类型,那么您可以将日期差异添加到另一个任意日期:

select date '1970-01-01' + (t2.time_event - t1.time_event) as ...

固定日期可以是任何东西,所以我刚刚选择了Unix纪元。

快速演示:

alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';

with TBL_DUMMYFEED as (
  select 1 as trax_id, 'created' as event,
    to_date('2015-02-25 06:49:15', 'YYYY-MM-DD HH24:MI:SS') as time_event
  from dual
  union all
    select 1 as trax_id, 'sent' as event,
    to_date('2015-02-25 08:13:47', 'YYYY-MM-DD HH24:MI:SS') as time_event
  from dual
)
select t2.time_event - t1.time_event as raw_diff,
   (t2.time_event - t1.time_event) * 24 * 60 * 60 as diff_in_seconds,
   numtodsinterval(t2.time_event - t1.time_event, 'DAY') as diff_interval,
   date '1970-01-01' + (t2.time_event - t1.time_event) as fake_date
from TBL_DUMMYFEED t1 
join TBL_DUMMYFEED t2 on t1.trax_id = t2.trax_id
where t1.event = 'created'
and t2.event = 'sent';

   RAW_DIFF DIFF_IN_SECONDS DIFF_INTERVAL FAKE_DATE          
----------- --------------- ------------- -------------------
.0587037037            5072 0 1:24:32.0   1970-01-01 01:24:32 

坚持原始数字或使用间隔数据类型更整洁,但似乎不符合您的设计约束。