So I am trying to insert multiples rows of data from one table to another. I have done this; however, I am having an issue with some of my columns, specifically my date colu
Two options:
1) If you have alter session privilege, change the NLS_DATE_FORMAT before running the SELECT
statement as given below:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-RR HH24:MI';
2) If you don't have ALTER session privilege then apply the transformation for each date field as given below (the stmnt below shows it for START_POUR)
'TO_DATE(''' || TO_CHAR(START_POUR, 'DD-MON-RR HH24:MI') || ''', ''DD-MON-RR HH24:MI'')'
First, do you have some sample data of the date fields?
If the information about the timestamp isn't stored it will do little good to call it.
The main issue I would look into it the format of the source you're pulling from.
From there you can set the format of the SELECT statement's output.
I wonder if you are making this problem more complicated than it needs to be.
There is the simple form of INSERT
where you need need to provide a VALUES
clause, but you can also insert the results of a SELECT
statement directly.
So you could just write something like:
insert into dante2 (
subcar, batch_id, silicon, temperature, sulphur, manganese,
phosphorus, start_pour, end_pour, sched_cast_date
)
SELECT
SUBCAR, BBP.BATCH_ID, SILICON, TEMPERATURE, SULPHUR, MANGANESE,
pHOSPHORUS, START_POUR, END_POUR, SCHED_CAST_DATE
FROM
bof_chem_sample bcs,
bof_batch_pour bbp,
bof_celox_sample bofcs
WHERE
bcs.SAMPLE_CODE= to_char('D1') and
bofcs.sample_code=bcs.sample_code and
bofcs.batch_id=bcs.batch_id and
bcs.batch_id = bbp.batch_id and
bofcs.temperature>0 AND
bbp.START_POUR>=to_date('01-JAN-2011', 'dd-mon-yyyy');
This will have the advantage of being much faster than your existing solution since each of the INSERT statements that you were running would take time to parse and execute but this has only single statement to run. It also runs in one step since you don't need to create the INSERT statements and all the types (like dates) are handled correctly.
However if you still need to create insert statements then you need to handle the data types. Strings and numbers are fine because Oracle knows what to do with them. dates are more of an issue because unless you use the to_char
and to_date
functions then you are relying on implicit conversion which will cause you mysterious problems like the ones you are experiencing. It's probably worth reading the Oracle documentation on implicit conversion to find out why it is bad.
As already mentioned by Cybernate, you would need to explicitly convert the date/time columns firstly to a character string (using to_char) that can then be converted back to a date by using to_date.