0

我有一个问题...如何计算 sysdate 和名为“创建时间”的列之间的差异?这是 SQL 的一部分(我正在使用 oracle 数据库):

$g->select_command = "select c.case_id as \"NGM ID\",   
       s.NE_PRIORITAET as \"NE Prio\",
       case substr(s.NE_ID, 2,1)
         when '1' then 'Nord'
         when '2' then 'Nord' 
         when '3' then 'Ost'
         when '4' then 'Ost'
         when '5' then 'Mitte'
         when '6' then 'West'
         when '7' then 'Süd'
         when '8' then 'Mitte'
         when '9' then 'Süd'
         else          'Error'
       end as \"Region\",         
       c.STATUS_NGM as \"NGM Status\",
       s.AUFTRAG as \"Auftrag\",
       s.NE_ID as \"NE ID\",
       s.STATUS as \"SAP Status\",
       substr(to_char(to_timestamp(Sysdate, 'YYYY/MM/DD HH24:MI:SS') - to_timestamp(s.CREATION_TIME, 'YYYY/MM/DD HH24:MI:SS'), 'YYYY/MM/DD HH24:MI:SS'), 8) as \"Diff Beginn Sap Ende\",
       case trim(s.KATEGORIE)
         when '1' then 'INSLA'
         when '2' then 'OUTSLA'
         else          s.KATEGORIE
       end as \"SLA\",
       c.CREATION_TIME as \"NGM Creation Time\",
4

2 回答 2

2

如果您想获得两个日期之间的差异,您可以简单地使用减法运算,如下所示:

with s as (
  select to_date('2014-01-01', 'YYYY-MM-DD') CREATION_TIME from dual
)
select sysdate - s.CREATION_TIME
  from s

DIFF
--------------
226.6314236111
于 2014-08-15T15:00:10.120 回答
1

在 Oracle 中,减去日期和时间戳时有两种不同的数据类型。两个 DATE 数据类型之间的距离是一个数字,其中一个单位代表天。两个 TIMESTAMP 数据类型之间的距离是 INTERVAL DAY TO SECOND。它是一种以天:小时:分钟:秒格式进行可视化的数据类型。

select sysdate-to_date('2014.08.15','YYYY.MM.DD') as date_type
     , systimestamp - to_timestamp('2014.08.15','YYYY.MM.DD') as ts_type
     , dump(sysdate-to_date('2014.08.15','YYYY.MM.DD')) as date_datatype
     , dump(systimestamp - to_timestamp('2014.08.15','YYYY.MM.DD')) as ts_datatype
  from dual;

 DATE_TYPE
----------
TS_TYPE
---------------------------------------------------------------------------
DATE_DATATYPE
--------------------------------------------------------------------------------
TS_DATATYPE
--------------------------------------------------------------------------------
,972719907
+000000000 23:20:43.608000
Typ=14 Len=8: 0,0,0,0,75,72,1,0
Typ=190 Len=24: 0,0,0,0,23,0,0,0,20,0,0,0,43,0,0,0,0,88,61,36,10,0,0,0
于 2014-08-15T21:25:40.557 回答