Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Oracle to_date function for date string with milliseconds

Tags:

oracle

to-date

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?

Thanks in advance!

like image 824
Luixv Avatar asked Feb 07 '12 16:02

Luixv


People also ask

What is the difference between TO_DATE and TO_CHAR in Oracle?

To_char formats a DATE into a string using the given format mask. To_date converts a STRING into a date using the format mask.

How does TO_DATE work in Oracle?

TO_DATE converts char of CHAR , VARCHAR2 , NCHAR , or NVARCHAR2 datatype to a value of DATE datatype. The fmt is a datetime model format specifying the format of char . If you omit fmt , then char must be in the default date format. If fmt is J , for Julian, then char must be an integer.

Does Oracle date include milliseconds?

Oracle to_char date format milliseconds Milliseconds is microseconds divided by 1000 ( milliseconds = microseconds / 1000 ). And 1 second has 1000 milliseconds. Oracle date data types do not support milliseconds but Oracle timestamps do.

How do I change date format from YYYY MM DD in Oracle?

SELECT TO_CHAR ( TO_DATE (date_value, 'yyyy-mm-dd') , 'mm/dd/yyyy' ) FROM table_x; Thank you,!!!! That did the trick.


2 Answers

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' ) 
like image 139
Justin Cave Avatar answered Sep 17 '22 14:09

Justin Cave


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.

like image 34
Mark J. Bobak Avatar answered Sep 20 '22 14:09

Mark J. Bobak