Importing a CSV to MySQL with different date format

You can replace format during importing data from the CSV file, for example –

LOAD DATA INFILE 'file_name.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(id, column2, column3, @date_time_variable) -- read one of the field to variable
SET date_time_column = STR_TO_DATE(@date_time_variable, '%d-%b-%Y'); -- format this date-time variable

It will format the string like ’31-Jan-2011′ to a correct DATETIME data type.

More information here – LOAD DATA INFILE Syntax.

Leave a Comment