How to load date data in MySQL when using LOAD DATA

后端 未结 3 941
南笙
南笙 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:40
    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))));
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-11 10:53

    Since I had a similar problem, but the incoming date format was different, here is what you would do should your incoming date format be the following,

    Sample input date format,

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

    You would need to use a different date format,

    %W, %b %d, %Y
    

    Here is a link to the mysql documentation and a list of date formats, but briefly,

    Date part,

    %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, ?-)
    

    Weekday,

    %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)
    

    Time

    %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
    

    There are other patterns which are not listed above.

    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');
    
    0 讨论(0)
提交回复
热议问题