2010-12-14 9 views
3
SQL> select to_timestamp('2010-12-14:09:56:53') - to_timestamp('2010-12-14:09:56:46') from dua 
l; 
select to_timestamp('2010-12-14:09:56:53') - to_timestamp('2010-12-14:09:56:46') from dual 
        * 
ERROR at line 1: 
ORA-01843: not a valid month 


SQL> select to_date('2010-12-14:09:56:53') - to_date('2010-12-14:09:56:46') from dual; 
select to_date('2010-12-14:09:56:53') - to_date('2010-12-14:09:56:46') from dual 
       * 
ERROR at line 1: 
ORA-01861: literal does not match format string 

"wörtliche Format-String stimmt nicht überein" Was ist der richtige Weg, um eineORA-01843 "kein gültiger Monat" und ORA-01861

2010-12-14:09:56:53 
minus 
2010-12-14:09:56:46 

in Oracle SQL zu tun?

+1

Dies ist wahrscheinlich nicht markiert sqlplus werden sollte - wie etwa SQL statt –

Antwort

12

Sie haben Format angeben, die Ihre Zeitstempel Zeichenfolge in zu TO_DATE und TO_TIMESTAMP Funktionen:

select 
    to_date('2010-12-14:09:56:53', 'YYYY-MM-DD:HH24:MI:SS') - 
    to_date('2010-12-14:09:56:46', 'YYYY-MM-DD:HH24:MI:SS') 
from dual; 

Ergebnis wird in Tagen, die man von 86.400 vermehren können Erhalten Sie Sekunden:

TO_DATE('2010-12-14:09:56:53','YYYY-MM-DD:HH24:MI:SS')-TO_DATE('2010-12-14:09:56 
-------------------------------------------------------------------------------- 
.000081019 

Mit TO_TIMESTAMP:

select 
    to_timestamp('2010-12-14:09:56:53', 'YYYY-MM-DD:HH24:MI:SS') - 
    to_timestamp('2010-12-14:09:56:46', 'YYYY-MM-DD:HH24:MI:SS') 
from dual; 

Ergebnis wird in TIMESTAMP Format:

TO_TIMESTAMP('2010-12-14:09:56:53','YYYY-MM-DD:HH24:MI:SS')-TO_TIMESTAMP('2 
--------------------------------------------------------------------------- 
+000000000 00:00:07.000000000 
+0

+1 für die Anzeige aller Variationen –

2

Verwenden to_timesatmp

select TO_TIMESTAMP('2010-12-14:09:56:53', 'YYYY-MM-DD:HH24:MI:SS.FF') 
- TO_TIMESTAMP('2010-12-14:09:56:46', 'YYYY-MM-DD:HH24:MI:SS.FF') 

from dual 
+0

Dank, das funktioniert. Wie kann ich das Ergebnis in Sekunden erzielen? – Moeb

+0

VLS Antwort geht ziemlich gut ins Detail, wie dies zu tun –

Verwandte Themen