我有一个问题......如何计算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\",
答案 0 :(得分: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
答案 1 :(得分: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