2012-01-12 13 views
8

Come posso fare questo:Oracle: come filtrare per data e ora in una clausola where

select * 
from tableName 
where SESSION_START_DATE_TIME > To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi') 

SESSION_START_DATE_TIME è nel formato '12/01/2012 13: 16: 32.000'

ho provato where To_Date (SESSION_START_DATE_TIME, 'DD-MON-YYYY hh24:mi') > To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi')

ma non importa quello che provo ottengo l'errore: SQL command not properly formed

+1

Il campo "SESSION_START_DATE_TIME' a DATETIME o VARCHAR? – MatBailie

+0

Ci sono parti dell'istruzione SQL che non ci stai mostrando? – wweicker

+0

'DESC TABLENAME;'? La tua query funzionerà perfettamente se il campo di tipo 'TIMESTAMP' –

risposta

0

Ovviamente '12/01/2012 13: 16: 32.000' non corrisponde 'DD-MO Formato N-YYYY hh24: mi '.

Aggiornamento:

È necessario 'MM/DD/YYYY HH24: mi: ss.ff' formato e di utilizzare TO_TIMESTAMP invece di date causa TO_DATE non tenere Millis in Oracle.

+0

sì, ecco perché ho provato il secondo modo nel mio post. Sto provando tutto quello che riesco a pensare! – Bob

+0

Ho aggiornato la risposta. – Vadzim

0

Prova:

To_Date (SESSION_START_DATE_TIME, 'MM/DD/YYYY hh24:mi') > 
To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi') 
+0

stesso errore ho paura – Bob

5

Nell'esempio che avete fornito non v'è nulla che possa lanciare un errore SQL command not properly formed. Come stai eseguendo questa query? Cosa non ci stai mostrando?

Questo script di esempio funziona bene:

create table tableName 
(session_start_date_time DATE); 

insert into tableName (session_start_date_time) 
values (sysdate+1); 

select * from tableName 
where session_start_date_time > to_date('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi'); 

come fa questo esempio:

create table tableName2 
(session_start_date_time TIMESTAMP); 

insert into tableName2 (session_start_date_time) 
values (to_timestamp('01/12/2012 16:01:02.345678','mm/dd/yyyy hh24:mi:ss.ff')); 

select * from tableName2 
where session_start_date_time > to_date('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi'); 

select * from tableName2 
where session_start_date_time > to_timestamp('01/12/2012 14:01:02.345678','mm/dd/yyyy hh24:mi:ss.ff'); 

Quindi ci deve essere qualcosa che è sbagliato.

5

Se SESSION_START_DATE_TIME è di tipo TIMESTAMP, è possibile provare a utilizzare la funzione SQL TO_TIMESTAMP. Ecco un esempio:

 SQL> CREATE TABLE t (ts TIMESTAMP); 

    Table created. 

    SQL> INSERT INTO t 
     2  VALUES (
     3     TO_TIMESTAMP (
     4     '1/12/2012 5:03:27.221008 PM' 
     5     ,'mm/dd/yyyy HH:MI:SS.FF AM' 
     6    ) 
     7    ); 

    1 row created. 

    SQL> SELECT * 
     2 FROM t 
     3 WHERE ts = 
     4   TO_TIMESTAMP (
     5    '1/12/2012 5:03:27.221008 PM' 
     6    ,'mm/dd/yyyy HH:MI:SS.FF AM' 
     7   ); 
    TS 
    ------------------------------------------------- 
    12-JAN-12 05.03.27.221008 PM 
0

Mettiamola così

where ("R"."TIME_STAMP">=TO_DATE ('03-02-2013 00:00:00', 'DD-MM-YYYY HH24:MI:SS') 
    AND "R"."TIME_STAMP"<=TO_DATE ('09-02-2013 23:59:59', 'DD-MM-YYYY HH24:MI:SS')) 

Dove R è il nome della tabella.
TIME_STAMP è FieldName nella tabella R.

Problemi correlati