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
07-JUN-12
or 07-Jun-12
) or %d-%b-%y
) orYour 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With