2013-09-20 62 views
8

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 | 
+------+------+ 

是與

  1. 輸入日期列的問題(應該是07-JUN-1207-Jun-12)或
  2. 用我的格式字符串(%d-%b-%y)或
  3. 別的東西嗎?
+2

類似http://stackoverflow.com/questions/10102167/load-data-infile-easily-convert-yyyymmdd-to-yyyy-mm-dd – zamnuts

+0

對不起你給的鏈接並不能解決問題。我創建了一個小測試來顯示問題: – smarisetti

回答

8

您的格式字符串STR_TO_DATE()無效。樣本數據中的小時數爲24小時格式(%H%k),而不是12小時(%h)。您可以看到所有可能的日期格式說明符here

變化

%d-%b-%y %h:%i:%s 

%d-%b-%y %H:%i:%s 
     ^^ 

你的語句可能看起來像這樣

LOAD DATA INFILE '/path/to/temp_test.csv' 
IGNORE INTO TABLE temp_test 
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\r\n' -- or '\n' 
    IGNORE 1 LINES 
(@c1, c2) 
SET c1 = STR_TO_DATE(@c1,'%d-%b-%y %H:%i:%s'); 

與樣品數據加載後

 
mysql> select * from temp_test; 
+---------------------+------+ 
| c1     | c2 | 
+---------------------+------+ 
| 2012-06-07 22:50:19 | abc | 
| 2013-06-07 22:50:19 | bcd | 
+---------------------+------+ 
2 rows in set (0.00 sec) 
+0

@smarisetti它有幫助嗎? – peterm

2

因爲我也有類似的問題,但傳入的日期格式是不同的,這裏是你會做什麼,應該將傳入的日期格式是以下,

樣品輸入日期格式,

Tuesday, May 24, 2016 
Wednesday, May 25, 2016 

你需要使用不同的日期格式,

%W, %b %d, %Y 

這裏是對mysql documentationlist of date formats一個鏈接,但簡要地說,

日期部分,

%Y Year, numeric, four digits 
%y Year, numeric (two digits) 
%c Month, numeric (0..12) 
%m Month, numeric (00..12) 
%M Month name (January..December) 
%b Abbreviated month name (Jan..Dec) 
%d Day of the month, numeric (00..31) 
%e Day of the month, numeric (0..31) 
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, ?-) 

平日,

%W Weekday name (Sunday..Saturday) 
%a Abbreviated weekday name (Sun..Sat) 
%w Day of the week (0=Sunday..6=Saturday) 
%j Day of year (001..366) 

時間

%T Time, 24-hour (hh:mm:ss) 
%r Time, 12-hour (hh:mm:ss followed by AM or PM) 
%H Hour (00..23) 
%h Hour (01..12) 
%I Hour (01..12) 
%k Hour (0..23) 
%l Hour (1..12) 
%i Minutes, numeric (00..59) 
%S Seconds (00..59) 
%s Seconds (00..59) 
%f Microseconds (000000..999999) 
%p AM or PM 

有其上面沒有列出的其他圖案。

LOAD DATA INFILE '/path/to/temp_test.csv' 
IGNORE INTO TABLE temp_test 
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\r\n' #use '\n' for linux 
    IGNORE 1 LINES 
(@var_col1, col2) 
SET col1 = STR_TO_DATE(@var_col1,'%d-%b-%y %H:%i:%s'); 
0
LOAD DATA INFILE '/path/to/temp_test.csv' 
IGNORE INTO TABLE temp_test 
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\r\n' -- or '\n' 
    IGNORE 1 LINES 
(@c1, c2) 
SET c1 = IF(CHAR_LENGTH((@c1)) = 0, NULL, (CONCAT(SUBSTRING(@c1e,7,4),SUBSTRING(@c1,3,4),SUBSTRING(@c1,1,2),SUBSTRING(@c1,11,9)))); 
+0

請格式化您的代碼,它不可讀。 – VPK

相關問題