I know this question has been asked before but for some reason I am having zero luck still. I am trying to import a CSV file and convert the date field to proper mysql format (YYYY-MM-DD). I should note that some of the records in the csv have have an empty value, ”, for a date value. Not sure if that means anything. I also should mention that I already have recordd in this table. It is not empty. I tried running this but had no luck
load data local infile '/Users/Path/To/CSV/file.csv'
into table donor fields terminated by ','
lines terminated by '\n' (@date,INSTRUCTIONS)
set date = STR_TO_DATE(@date, '%Y-%m-%d')
I’m getting NULL returned for all of them. Any Ideas?
If you’re doing this only once, and the table is empty to start, you could run the import but first alter your table so the date column is of type VARCHAR. Then run
UPDATE table SET date = str_to_date( date, '%m/%d/%Y'), and convert the column back to DATETIME or DATE.You could alternatively add a second date column with format DATE, import into the first date column in format VARCHAR, and run
UPDATE table SET date2 = str_to_date( date1, '%m/%d/%Y')and then DROP the varchar column.It’s important to note that the second parameter of
STR_TO_DATE()is the inputted format, not the resulting format. So in your second parameter, you’re instructing MySQL that the date format in the CSV is Y-m-d. If it is in fact m/d/Y, you ought to be using `STR_TO_DATE(@date_date, ‘%m/%d/%Y)’.Hope this helps…