Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract only Time from a DateTime field in Oracle SQL Developer?

I tried To_Timestamp and other methods for SQL Developer but only this one worked fine for me.

Select To_Number(To_Char(DateTime_FieldName, 'HH24'))
    || ':' || to_number(to_char(DateTime_FieldName, 'MI'))
    || ':' ||to_number(to_char(DateTime_FieldName, 'SS'))
from TABLE_NAME

Is there a better solution to this?

like image 304
MontyPython Avatar asked Jun 14 '13 08:06

MontyPython


People also ask

How do I show time in SQL Developer?

Go to Tools -> Preferences -> Database -> NLS and change Date Format value to DD-MON-RR HH24:MI:SS (for 24 hour time display) or DD-MON-RR HH:MI:SS (for 12 hour time display).

Is there a time datatype in Oracle?

Oracle Database has five date-time data types: TIMESTAMP. TIMESTAMP WITH TIME ZONE. TIMESTAMP WITH LOCAL TIME ZONE.


1 Answers

Assuming your goal is to generate a string representing the time (which is what the query you posted returns despite the extraneous to_number calls)

SELECT to_char( <<column_name>>, 'HH24:MI:SS' )
  FROM table_name

If you want to return a different data type, you'd need to tell us what data type you want to return. If, for example, you really want to return an INTERVAL DAY TO SECOND

SELECT numtodsinterval( <<column name>> - trunc(<<column name>>), 'day' )
  FROM table_name
like image 196
Justin Cave Avatar answered Oct 17 '22 07:10

Justin Cave