Oracle to MySQL timestamp conversion in MySQL insert statement

孤街醉人 提交于 2021-01-28 18:12:31

问题


I've already exported data from an Oracle 11g instance as insert statements. They need to go into a MySQL 5.5 instance. Having trouble with the TIMESTAMP conversion; I know I can use the TO_TIMESTAMP function inline with INSERT statements in MySQL; however, am unsure as to the correct flags to use. As follows below, in linear order, is: the Oracle timestamp statement and my attempt at a MySQL compatible statement. Obviously it's not working (in that MySQL states there's a syntax error).

Oracle:

TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','fmMMfm/fmDDfm/YYYY fmHH12fm:MI:SS.FF AM')

MySQL:

TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','%m/%d/%Y %h:%i:%s')

What am I missing beyond flags for microseconds and AM/PM?


回答1:


assumed you export from SQL Developer.

before you export the whole data, change your datetime select query with :

TO_CHAR(COLUMN_NAME, 'YYYY-MM-DD HH24:MI:SS') as XXX

it will produce datetime with MySQL format. so when you get the insert query, you can run directly at MySQL




回答2:


FOR MYSQL: STR_TO_DATE('2/6/2015 5:20:43.000000 AM','%c/%d/%Y %l:%i:%s.%f %p')




回答3:


if you want the date and the timestamp of oracle to be compatible with MySql just alter you session

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH:MI:SS.FF'

it is better to alter session



来源:https://stackoverflow.com/questions/38125393/oracle-to-mysql-timestamp-conversion-in-mysql-insert-statement

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