共計 2281 個字符,預計需要花費 6 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 mysql load 報錯 Incorrect datetime value 怎么辦,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
有個需求要從 oracle 把數(shù)據(jù)倒進 mysql,
方法一:用 spool 導成 txt 文件,然后 load 進去 mysql
oracle 源端:spool 配置
[oracle11@S248 ~]$ cat spool_bak.sql
set colsep |
SET feedback off
SET newpage none
SET pagesize 50000
SET linesize 20000
SET verify off
SET pagesize 0
SET term off
SET trims ON
SET heading off
SET trimspool ON
SET trimout ON
SET timing off
SET verify off
spool /oracle/spool1.txt
select swebid,dstart from liuwenhe.fukuandate where dstart is not null and swebid is not null ;
spool off
EOF
運行 spool 文件,生成 txt 文件
SQL @spool_bak.sql
把生成的 txt 文件傳到 mysql 服務器下。
然后執(zhí)行 load 進去 mysql
mysql LOAD DATA INFILE C:\\Users\\manet\\Desktop\\spool.txt INTO TABLE fukuandate FIELDS TERMINATED BY |
不幸的是報錯:
Incorrect datetime value: 15-SEP-15 for column DSTART at row 1
很明顯是日期格式的問題,查看導出來的 txt 文件內(nèi)容:
[oracle11@S248 ~]$ cat spool1.txt
403965|15-SEP-15
442917|01-AUG-16
然而 mysql 的默認日期格式:為 yyy-mm-dd
mysql show variables like datetime_format #### 查看 mysql 的默認格式
+—————–+——————-+
| Variable_name | Value |
+—————–+——————-+
| datetime_format | %Y-%m-%d %H:%i:%s |
+—————–+——————-+
1 row in set (0.00 sec)
mysql SELECT NOW() FROM DUAL;
+———————+
| NOW() |
+———————+
| 2017-03-03 13:32:18 |
+———————+
1 row in set (0.00 sec)
于是猜想把數(shù)據(jù)中的日期改成 yyyy-mm-dd 的形式, 修改 oraclespool.sql 腳本,把日期格式改成 yyyy-mm-dd 的形式。
[oracle11@S248 ~]$ cat spool.sql
set colsep |
SET feedback off
SET newpage none
SET pagesize 50000
SET linesize 20000
SET verify off
SET pagesize 0
SET term off
SET trims ON
SET heading off
SET trimspool ON
SET trimout ON
SET timing off
SET verify off
spool /oracle/spool.txt
select swebid,to_char(dstart, yyyy-mm-dd) from liuwenhe.fukuandate where dstart is not null and swebid is not null;
spool off
EOF
然后從新執(zhí)行 spool.sql 腳本,新生成的 txt 文件,再次 load 進去,成功了。。
mysql LOAD DATA INFILE C:\\Users\\manet\\Desktop\\spool.txt INTO TABLE fukuandate FIELDS TERMINATED BY |
共 73506 行受到影響
方法二:注意這種方法僅僅適合于數(shù)據(jù)量比較小的情況,因為數(shù)據(jù)量大的話,你沒辦法編輯 sql 文件。用 plsql developer 工具導成 sql 文件,然后修改 sql 文件,倒進 mysql。
在 plsql developer 工具里,在左側找到需要導出的表,然后 - 右擊 - 選擇導出數(shù)據(jù),之后選擇路徑,如下圖:
然后編輯導出的 sql 文件,使它能正確的在 mysql 中執(zhí)行,to_date 替換成 STR_TO_DATE
insert into FUKUANDATE (SWEBID, DSTART)
values (403965, to_date( 15-09-2015 , dd-mm-yyyy
改成:
INSERT INTO FUKUANDATE (SWEBID, DSTART) VALUES (403965,STR_TO_DATE( 15-09-2015 , %d-%m-%Y
然后執(zhí)行即可,
以上是“mysql load 報錯 Incorrect datetime value 怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業(yè)資訊頻道!