How to change string date to MySQL date format at time of import of CSV using MySQL's LOAD DATA LOCAL INFILE

后端 未结 2 458
Happy的楠姐
Happy的楠姐 2020-12-01 16:18

I\'m using MySQL\'s LOAD DATA LOCAL INFILE SQL statement to load data from a CSV file into an existing database table.

Here is an example SQL statement:



        
2条回答
  •  温柔的废话
    2020-12-01 17:06

    A slightly more lengthy process that gives you a bit of testing flexibility:

    • Use a VARCHAR(64) column callsed eg. mydate_text to hold the unformatted date you import.
    • Load up/import the table putting the date into this plain text field
    • Run a query like

      UPDATE mytable SET mydate = STR_TO_DATE(mydate_text, '%Y-%b-%d')

    • If it all looks OK, drop your mydate_text column

    • If it's not OK, simply try again with a new format.

    The advantage to this technique is it lets you play around with the formats without having to re-import the table using the somewhat fussy MySQL LOAD DATA syntax, especially with length table columns. If you know exactly what you're doing, the answer above is best. If you're not an expert in MySQL, this technique can be helpful till you get your formats exactly right.

提交回复
热议问题