MySQL中的日期列的默认日期格式为YYYY-MM-DD HH:MM:SS
。
我尝试加载的数据文件的日期字段的日期格式为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
)或答案 0 :(得分:9)
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)
答案 1 :(得分:4)
由于我遇到了类似的问题,但传入日期格式不同,如果您的传入日期格式如下,您可以采取以下措施,
示例输入日期格式
Tuesday, May 24, 2016
Wednesday, May 25, 2016
您需要使用其他日期格式
%W, %b %d, %Y
以下是指向mysql documentation和list 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');
答案 2 :(得分:-1)
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))));