Default Date Format: YYYY-MM-DD :
mysql> SELECT @@date_format; +---------------+ | @@date_format | +---------------+ | %Y-%m-%d | +---------------+
... therefore MySQL will not recognize things like 11/17/2012 as the correct date. In theory, you should be able to change the default format, but I'm not sure that this can be done in the session area, and I would not recommend changing it for the entire server. It is best to do the conversion. The trick is to insert the value into the variable, not the column.
In addition, there are two more problems:
- The CSV file contains a header line.
- Your fields are not shared.
Assuming your file uses tabs as delimiters, the complete command is:
LOAD DATA LOCAL INFILE '/path/to/csv/file.csv' INTO TABLE mytable FIELDS TERMINATED BY '\t' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (id, task, hoursWorked, @tmp_begindate, @tmp_enddate) SET begindate = STR_TO_DATE(@tmp_begindate, '%m/%d/%Y'), enddate = STR_TO_DATE(@tmp_enddate, '%m/%d/%Y');
MySQL actually does not allow changing @@date_format in any way:
mysql> SET @@date_format='%d/%m/%Y'; ERROR 1238 (HY000): Variable 'date_format' is a read only variable
As explained by MySQL 5.6 manual :
This variable is not used. It has been deprecated since MySQL 5.6.7 and will be removed in a future release of MySQL.
In addition, in the "Date and Time Types" we can read:
MySQL retrieves values for a given date or time type in a standard output format, but it tries to interpret various formats for the input values that you supply (for example, when you specify a value for are assigned or matched to a date or time type). For a description of the allowed formats for date and time types, see Section 10.1.3, “Date and Time Literals” . You are expected to provide valid values. Unpredictable results may occur if you use values in other formats.
Although MySQL tries to interpret values in several formats, date details should always be specified in year-month-day order (for example, '98 -09-04 '), and not in month-day-year or day-month-year commonly used in other places (e.g. '09 -04-98 ', '04 -09-98').