溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql load報錯Incorrect datetime value怎么辦

發(fā)布時間:2021-11-06 15:37:05 來源:億速云 閱讀:829 作者:小新 欄目:MySQL數(shù)據(jù)庫

小編給大家分享一下mysql load報錯Incorrect datetime value怎么辦,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

有個需求要從oracle把數(shù)據(jù)倒進(jìn)mysql,

方法一:用spool導(dǎo)成txt文件,然后load進(jìn)去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服務(wù)器下。

然后執(zhí)行l(wèi)oad進(jìn)去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

很明顯是日期格式的問題,查看導(dǎo)出來的txt文件內(nèi)容:

[oracle11@S248 ~]$ cat spool1.txt

403965|15-SEP-15

442917|01-AUG-16

然而mysql的默認(rèn)日期格式:為yyy-mm-dd

mysql> show variables like 'datetime_format';       ####查看mysql的默認(rèn)格式

+-----------------+-------------------+

| 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進(jìn)去,成功了。。

mysql> LOAD DATA INFILE 'C:\\Users\\manet\\Desktop\\spool.txt'   INTO TABLE fukuandate  FIELDS TERMINATED  BY "|"

共 73506 行受到影響

方法二:注意這種方法僅僅適合于數(shù)據(jù)量比較小的情況,因為數(shù)據(jù)量大的話,你沒辦法編輯sql文件。用plsql developer 工具導(dǎo)成sql文件,然后修改sql文件,倒進(jìn)mysql。

在plsql developer 工具里,在左側(cè)找到需要導(dǎo)出的表,然后-右擊-選擇導(dǎo)出數(shù)據(jù),之后選擇路徑,如下圖:
mysql load報錯Incorrect datetime value怎么辦

mysql load報錯Incorrect datetime value怎么辦

然后編輯導(dǎo)出的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)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI