Change date format that MySQL reads

非 Y 不嫁゛ 提交于 2019-12-13 03:14:24

问题


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


来源:https://stackoverflow.com/questions/404875/change-date-format-that-mysql-reads

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!