Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load date data in MySQL when using LOAD DATA

The default date format of a date column is YYYY-MM-DD HH:MM:SS in MySQL.

The data file that I am trying load from has a date field that has the date in DD-MON-YY HH:MM:SS format. When I load this file using LOAD DATA command, the database gets confused and just makes all date entries to 0000-00-00 00:00:00 or NULL

Here is the test I did using STR_TO_DATE option and it doesn't work.

Test infile (test_temp.csv)

c1, c2
07-JUN-12 22:50:19, "abc"
07-JUN-13 22:50:19, "bcd"

Test table (temp_test)

describe temp_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| c1    | datetime    | YES  |     | NULL    |       |
| c2    | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

Data Load command:

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');

Output

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

Is the problem with

  1. Input date column (Should it be 07-JUN-12 or 07-Jun-12) or
  2. With my format string (%d-%b-%y) or
  3. Something else?
like image 551
smarisetti Avatar asked Sep 20 '13 23:09

smarisetti


1 Answers

Your format string for STR_TO_DATE() is invalid. Hours in your sample data have 24-hour format (%H or %k) instead of 12-hour (%h). You can see all possible date format specifiers here.

Change

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

to

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

Your statement might look like this

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');

After loading with your sample data

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)
like image 182
peterm Avatar answered Nov 10 '22 19:11

peterm