如何通过减去两个日期来获得小时和分钟?

时间:2014-10-30 06:07:30

标签: sql oracle

我希望获得两个日期之间的小时和分钟。但它没有工作,我收到此错误

    ORA-01843: not a valid month
    01843. 00000 -  "not a valid month"

这是脚本

     select q.name as queue_name
        ,to_date(t.create_time, 'dd-mon-yyyy hh24:mi:ss')
        ,to_date(t.close_time, 'dd-mon-yyyy hh24:mi:ss')
        ,NUMTOYMINTERVAL(t.close_time - t.create_time, 'DAY')*24
        ,NUMTOYMINTERVAL(t.close_time - t.create_time, 'DAY')*24*60
  from app_account.otrs_ticket t
  left join app_account.otrs_user u
  on t.create_user_id=u.id
  left join app_account.otrs_queue q
  on q.id=t.queue_id
  group by to_date(t.create_time, 'dd-mon-yyyy hh24:mi:ss')
          ,to_date(t.close_time, 'dd-mon-yyyy hh24:mi:ss')
          ,NUMTOYMINTERVAL(t.close_time - t.create_time, 'DAY')*24
        ,NUMTOYMINTERVAL(t.close_time - t.create_time, 'DAY')*24*60
        ,q.name
  order by to_date(t.create_time, 'dd-mon-yyyy hh24:mi:ss') desc; 

4 个答案:

答案 0 :(得分:1)

试试这个你至少会知道如何计算两个日期之间的时差。

   SELECT floor(((date1-date2)*24*60*60)/3600)
            || ' HOURS ' ||
             floor((((date1-date2)*24*60*60) -
           floor(((date1-date2)*24*60*60)/3600)*3600)/60)
            || ' MINUTES ' ||
            round((((date1-date2)*24*60*60) -
             floor(((date1-date2)*24*60*60)/3600)*3600 -
            (floor((((date1-date2)*24*60*60) -
           floor(((date1-date2)*24*60*60)/3600)*3600)/60)*60) ))
          || ' SECS ' time_difference
     FROM dates;

    TIME_DIFFERENCE
    --------------------------------------------------------------------------------
    24 HOURS 0 MINUTES 0 SECS
    1 HOURS 0 MINUTES 0 SECS
    0 HOURS 1 MINUTES 0 SECS

有关详细信息,请参阅此链接: Click Here

答案 1 :(得分:1)

使用NUMTODSINTERVAL(close_date - create_date, 'DAY') or NUMTOYMINTERVAL(close_date - create_date, 'DAY')计算天数差异。

小时数乘以24,分钟乘以24 * 60。

select q.name as queue_name
            ,to_date(t.create_time, 'dd-mon-yyyy hh24:mi:ss') as create_date
            ,to_date(t.close_time, 'dd-mon-yyyy hh24:mi:ss') as close_date
            ,NUMTODSINTERVAL(close_date - create_date, 'DAY') or NUMTOYMINTERVAL(close_date - create_date, 'DAY')*24
            ,NUMTODSINTERVAL(close_date - create_date, 'DAY') or NUMTOYMINTERVAL(close_date - create_date, 'DAY') *24*60
      from app_account.otrs_ticket t
      left join app_account.otrs_user u
      on t.create_user_id=u.id
      left join app_account.otrs_queue q
      on q.id=t.queue_id
      where q.name not like 'Facilities Management::%'
      and q.name not like 'HR::%'
      and q.name not like 'Raw%'
      and q.name not like 'Procurement::%'
      and q.name not like 'Facilities Management%'
      and q.name not like 'Junk%'
      and q.name not like 'Facility Request Test%'
      and q.name not like 'Misc%'
      and q.name not like 'POS::POS issue - need paper%'
      group by to_date(t.create_time, 'dd-mon-yyyy hh24:mi:ss') as create_date
              ,to_date(t.close_time, 'dd-mon-yyyy hh24:mi:ss') as close_date
      order by to_date(t.create_time, 'dd-mon-yyyy hh24:mi:ss') desc; 

答案 2 :(得分:0)

另一种方法是直接计算值而不使用任何oracle函数:

以下是一个例子:

WITH q AS
(SELECT TO_DATE(create_time, 'YYYY-MM-DD HH24:MI') AS d_start,
        TO_DATE(close_time, 'YYYY-MM-DD HH24:MI')        AS d_end
   FROM app_account.otrs_ticket
)
SELECT FLOOR((d_end - d_start) * 24)                                                   AS hours,
       FLOOR(((d_end     - d_start) * 24 * 60) - (floor((d_end - d_start) * 24) * 60)) AS minutes
  FROM q;

希望它有所帮助!

答案 3 :(得分:0)

将您的DATE(或其所有数据类型)投射到TIMESTAMP。差异为您提供INTERVAL值,您可以在其中提取所有信息。应该是这样的:

EXTRACT(DAY FROM CAST(t.close_time AS TIMESTAMP) - CAST(t.create_time AS TIMESTAMP))*24*60
+ EXTRACT(HOUR FROM CAST(t.close_time AS TIMESTAMP) - CAST(t.create_time AS TIMESTAMP))*60
+ EXTRACT(MINUTE FROM CAST(t.close_time AS TIMESTAMP) - CAST(t.create_time AS TIMESTAMP)) AS MINUTES