How to load date data in MySQL when using LOAD DATA

后端 未结 3 963
南笙
南笙 2021-01-11 09:49

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

3条回答
  •  一整个雨季
    2021-01-11 10:52

    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)
    

提交回复
热议问题