Change date format that MySQL reads

2019-08-31 08:50发布

问题:

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?

回答1:

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