81

I have to perform some inserts into an Oracle DB. I have some dates in the following format

'23.12.2011 13:01:001'

Following the documentation I wrote inserts to_date as follows:

to_date('23.12.2011 13:01:01', 'DD.MM.YYYY HH24:MI:SS')

which works properly. Now I have dates with milliseconds with the format

'23.12.2011 13:01:001'

I've tried the following:

to_date('23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3')

which is incorrect (delivers an error 01821. 00000 - "date format not recognized").

Which "String" should I use for this format with milliseconds?

starball
  • 20,030
  • 7
  • 43
  • 238
Luixv
  • 8,590
  • 21
  • 84
  • 121
  • 1
    possible duplicate of [String to date in Oracle with milliseconds](http://stackoverflow.com/questions/1758219/string-to-date-in-oracle-with-milliseconds) – Igby Largeman Feb 07 '12 at 16:47

5 Answers5

125

An Oracle DATE does not store times with more precision than a second. You cannot store millisecond precision data in a DATE column.

Your two options are to either truncate the string of the milliseconds before converting it into a DATE, i.e.

to_date( substr('23.12.2011 13:01:001', 1, 19), 'DD.MM.YYYY HH24:MI:SS' )

or to convert the string into a TIMESTAMP that does support millisecond precision

to_timestamp( '23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3' )
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
18

TO_DATE supports conversion to DATE datatype, which doesn't support milliseconds. If you want millisecond support in Oracle, you should look at TIMESTAMP datatype and TO_TIMESTAMP function.

Hope that helps.

Mark J. Bobak
  • 13,720
  • 6
  • 39
  • 67
12

For three digits millisecond:

TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF3')

For six digits millisecond:

TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF'),
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
SANKAR
  • 137
  • 1
  • 2
10

You can try this format SS.FF for milliseconds:

to_timestamp(table_1.date_col,'DD-Mon-RR HH24:MI:SS.FF')

For more details:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm

joym8
  • 4,014
  • 3
  • 50
  • 93
dipti
  • 117
  • 1
  • 2
-9

You have to change date class to timestamp.

String s=df.format(c.getTime());
java.util.Date parsedUtilDate = df.parse(s);  
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedUtilDate.getTime());