Reputation: 7228
I have a column in a csv file called created
with values:
2012-04-16 14:46:42
2012-06-16 12:40:52.653000000
I am trying to import this into Oracle 11gR2 with SQL Developer 3.1.07
I am using this as a format: RRRR-MM-DD HH24:MI:SS.FF
I get error input value not long enough for the date format
If I change the format to RRRR-MM-DD HH24:MI:SS
I get error SQL Error: ORA-01830: date format picture ends before converting entire input string
How should I do it?
Upvotes: 1
Views: 3605
Reputation: 146199
You have some dates and some timestamps. Such is the problem with importing data from systems with shonky data integrity.
You have two options: clean the data before importing in or clean during importation.
There are various different methods for cleaning the data beforehand. One option is to use a text processor or IDE which supports regular expressions, and do a search and replace to fix those values which lack the fractional seconds.
The easiest way of cleansing during import is to use an external table as an intermediary (rather than SQL Loader). External tables are highly neat objects which allow us to manipulate structured data in OS files (such as CSV) as though it is in the database. The big advantage of external tables over SQL Loader is that we can use them in DML statements. Find out more.
For this to work you will need to Define the external table with that column as a varchar2. You will also need to build a function which takes a string and converts it to a timestamp.
create or replace function fix_ts (str in varchar2)
return timestamp
is
is_a_date exception;
pragma exception_init(is_a_date, -1840);
rv timestamp;
begin
begin
rv := to_timestamp(str);
exception
when is_a_date then
rv := to_timestamp(str)||'.000000000';
end;
return rv;
end;
Then you can insert the record int your target table like this:
insert into target_table
(id, created, whatever)
select id
, fix_ts(created)
, whatever
from external_table;
If this is a one-off data cleansing exercise then you're probably better off munging the data in an IDE. If this is a regular data feed then it's worth your while to build something more autonomous and robust.
As per your comment there is a third option: to get the source system to fix the data during the export process. This can be easy or difficult, depending on many factors; quite often the political issues are harder to resolve than the technical ones.
Upvotes: 3