2013-03-18 63 views
1

領域中,我針對表 DATE_SENT DATESTR_TO_DATE在加載文件導致NULL值

這裏是CSV的部分和下面的導入查詢。爲什麼日期字段被設置爲NULL?

1,"1/21/2013","0",1 
2,"2/27/2013","0",1 

這是SQL

LOAD DATA LOCAL INFILE 'C:/Users/Me/CSV.csv' 
    INTO TABLE Collections 
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\r\n' 
(`id`, @DATE_STR, `col`, `sid`) 
SET `date_sent` = STR_TO_DATE(@DATE_STR, '%m/%d/%Y') 

回答

2

MySQL's dev pages

%m  Month, numeric (00..12) 
%d  Day of the month, numeric (00..31) 

您需要使用%c,而不是(可能%e太)。

%c  Month, numeric (0..12) 
%e  Day of the month, numeric (0..31) 
1

嘗試使用此日期格式:

STR_TO_DATE(@DATE_STR, '%c/%e/%Y') 
  • %c是月,數字,0..12%m格式爲00..12
  • %e月是一個月的一天,數字,0..31%d是格式00..31

請參閱文檔DATE_FORMAT