17

日期列的默认日期格式YYYY-MM-DD HH:MM:SS在 MySQL 中。

我尝试从中加载的数据文件有一个日期字段,其中包含日期DD-MON-YY HH:MM:SS格式。当我使用命令加载此文件时LOAD DATA,数据库会感到困惑,只会将所有日期条目设为0000-00-00 00:00:00NULL

这是我使用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. 还有什么?
4

3 回答 3

21

您的格式字符串STR_TO_DATE()无效。示例数据中的小时数采用 24 小时格式 (%H%k) 而不是 12 小时 ( %h)。您可以在此处查看所有可能的日期格式说明符。

改变

%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> 从 temp_test 中选择 *;
+------------------+------+
| c1 | c2 |
+------------------+------+
| 2012-06-07 22:50:19 | 美国广播公司 |
| 2013-06-07 22:50:19 | 光盘 |
+------------------+------+
2 行(0.00 秒)
于 2013-09-28T18:05:54.350 回答
9

由于我遇到了类似的问题,但传入的日期格式不同,如果传入的日期格式如下,您将执行以下操作,

示例输入日期格式,

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

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

%W, %b %d, %Y

这是mysql 文档的链接和日期格式列表,但简要地说,

日期部分,

%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');
于 2016-06-16T16:23:31.660 回答
-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))));
于 2017-03-03T06:22:28.330 回答