Question

I am using Oracle 11g and trying to figure out how to insert this date into my table. The date seems like it is ISO-8601, but the 7 zeros are confusing me.

Insert into myTestTable (myDate) values ('2013-01-22T00:00:00.0000000-05:00');

I have tried to format the date with no luck. The error I am getting is ORA-01861 literal does not match format string.

Was it helpful?

Solution 2

Here is the answer.

insert into myTestTable (myDate) values 
(to_timestamp_tz('2013-01-22T00:00:00.0000000-05:00',
 'YYYY-MM-DD"t"HH24:MI:SS.FF7TZR'));

OTHER TIPS

First of all, the column must be a TIMESTAMP WITH TIME ZONE type to hold the value you're trying to insert. An Oracle DATE is accurate to seconds only, and it doesn't hold a time zone.

The seven zeros are the fractional seconds. The default precision for a TIMESTAMP WITH TIME ZONE happens to be seven decimal places. To specify three decimal places for seconds, define the column as TIMESTAMP(3) WITH LOCAL TIME ZONE.

The actual number of decimal places returned by SYSTIMESTAMP, which is the current timestamp at the server, depends on the operating system. My local Windows 7 Oracle returns three significant decimal places, while the Solaris OS at one of my clients returns six significant decimal places.

As for inserting the value, if you do something like this...

insert into myTestTable (myTS) values ('2013-01-22T00:00:00.0000000-05:00');

... Oracle will try to convert the timestamp using its current NLS_TIMESTAMP_TZ_FORMAT setting. You can query the setting like this:

SELECT *
FROM NLS_Session_Parameters
WHERE Parameter = 'NLS_TIMESTAMP_TZ_FORMAT';

PARAMETER               VALUE
----------------------- ----------------------------
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR

The result I got is the "factory default". Yours is probably the same, and you can see it doesn't match the format you've given, hence the conversion fails.

As another answer correctly notes, you can use the TO_TIMESTAMP_TZ function and a format string to convert the string to a timestamp. You can also use an ANSI timestamp literal:

insert into myTestTable (myTS)
  values (TIMESTAMP '2013-01-22T00:00:00.0000000-05:00');

Oracle documents timestamp literals here. The link covers literals for all types, so you'll need to scroll about two-thirds of the way down the screen (or do a find) to get to the timestamp literals.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top