MySQL中日期列的默認日期格式爲YYYY-MM-DD HH:MM:SS
。如何在使用加載數據時在MySQL中加載日期數據
我試圖加載的數據文件的日期字段的格式爲DD-MON-YY HH:MM:SS
。當我使用LOAD DATA
命令加載此文件,數據庫會產生混亂而只是讓所有日期條目0000-00-00 00:00:00
或NULL
這裏是我使用STR_TO_DATE
選項做了測試,它無法正常工作。
測試的infile(test_temp.csv)
c1, c2
07-JUN-12 22:50:19, "abc"
07-JUN-13 22:50:19, "bcd"
測試表(temp_test)
describe temp_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| c1 | datetime | YES | | NULL | |
| c2 | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
數據加載命令:
load data
infile '/var/lib/mysql/DataSet-1/temp_test.csv'
ignore
into table temp_test
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 lines
(@var_c1,c2)
set c1 = STR_TO_DATE(@var_c1,'%d-%b-%y %h:%i:%s');
輸出
Query OK, 2 rows affected, 2 warnings (0.00 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
MySQL> show warnings;
+-------+------+-------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------+
| Error | 1411 | Incorrect datetime value: '07-JUN-12 22:50:19' for function str_to_date |
| Error | 1411 | Incorrect datetime value: '07-JUN-13 22:50:19' for function str_to_date |
+-------+------+-------------------------------------------------------------------------+
MySQL> select * from temp_test;
+------+------+
| c1 | c2 |
+------+------+
| NULL | abc |
| NULL | bcd |
+------+------+
是與
- 輸入日期列的問題(應該是
07-JUN-12
或07-Jun-12
)或 - 用我的格式字符串(
%d-%b-%y
)或 - 別的東西嗎?
類似http://stackoverflow.com/questions/10102167/load-data-infile-easily-convert-yyyymmdd-to-yyyy-mm-dd – zamnuts
對不起你給的鏈接並不能解決問題。我創建了一個小測試來顯示問題: – smarisetti