2014-01-14 72 views

回答

1

EXTRACT作品有關日期時間或間隔值表達式。當你減去兩個日期時,你會得到一個數字,這是數字天數。如果您想使用 提取,則應使用NUMTODSINTERVAL將其轉換爲區間數據類型,然後使用EXTRACT。

SELECT TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') 
     - TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss') 
    FROM DUAL; 

0.6453125 

SELECT NUMTODSINTERVAL (
      TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') 
      - TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss'), 
      'DAY') 
    FROM DUAL; 

+00 15:29:15.000000 

SELECT EXTRACT (HOUR FROM intrvl) 
     || ':' 
     || EXTRACT (MINUTE FROM intrvl) 
     || ':' 
     || EXTRACT (SECOND FROM intrvl) 
    FROM (SELECT NUMTODSINTERVAL (
        TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') 
        - TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss'), 
        'DAY') 
        AS intrvl 
      FROM DUAL); 

15:29:15 

您甚至可以將日期轉換爲時間戳,以便差別已經在時間間隔數據類型中。

SELECT EXTRACT (HOUR FROM intrvl) 
     || ':' 
     || EXTRACT (MINUTE FROM intrvl) 
     || ':' 
     || EXTRACT (SECOND FROM intrvl) 
    FROM (SELECT CAST (
        TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') AS TIMESTAMP) 
       - CAST (
        TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss') AS TIMESTAMP) 
        AS intrvl 
      FROM DUAL);