tags:

views:

1244

answers:

1

I have a batch file that was created for an Oracle database that I'm now trying to import to a MySQL database. I've had to deal with all sorts of inconsistencies between the two. The file has all the dates as '17-NOV-1981' rather than '1981-11-17' as MySQL expects.

The batch file had the command *ALTER SESSION set nls_date_format='DD-MON-YYYY';* to set the date format. Is there an equivalent in MySQL to get it to accept the dates as is without having to majorly edit the file?

+2  A: 

I don't believe it's possible without modifying the SQL.

STR_TO_DATE could be used in the insert statements to convert the strings, or you could try piping the file through sed and use a regex to spot dates and rework them, e.g. something like this would replace single quoted dates in your original dump with calls to str_to_date, and pipe the resulting transformed sql into mysql...

cat oracledump.sql |  sed "s/'[0-9][0-9]-\(JAN\|FEB\|MAR\|APR\|MAY\|JUN\|JUL\|AUG\|SEP\|OCT\|NOV\|DEC\)-[1-2][0-9][0-9][0-9]'/str_to_date(&, '%d-%b-%Y')/g" | mysql newdb
Paul Dixon
I changed the end to > output.sql but all I got was the exact same file. The dates had not been changed.
thornate
Maybe you need to tweak the regex for your data, I did test it on a sample file before posting though. Why note post a fragment of your SQL?
Paul Dixon
Paul Dixon
No, they were single quote dates. A full line example is as follows: INSERT INTO ord (TOTAL, SHIPDATE, ORDID, ORDERDATE, CUSTID, COMMPLAN) VALUES ('710', '06-FEB-2002', '615', '01-FEB-2002', '107', '');
thornate
Paul Dixon
Hurrah! That worked great. I did have to amend it again after I realised that dates between 1 and 9 were printed with only one digit, but now it's all uploaded correctly. Thanks.
thornate