load/import fails in mysql due to date format - mysql

I am importing data from a .csv file into the table in MySQL, in file there is multiple date columns in the format of 04-05-2017 , which MySQL doesn't accepts.
It fails saying
ERROR 1292 (22007): Incorrect date value: '04-05-2017' for column 'START_DATE' at row 1
Please note that my
|START_DATE | date |
is a date column.
Thanks

Try it like this:
LOAD DATA INFILE 'file.csv'
INTO TABLE t1
FIELDS TERMINATED BY ','
(column1, #var1, column3, ...)
SET column2 = STR_TO_DATE(#var1,'%d-%m-%Y')
Replace the column with a variable. Then in the SET command you convert your string to a proper date.
read more about load data infile here
and here is more information about str_to_date()
and finally more information about using variables, if you need to have those

Related

Create table with default dateformat?

I am using a Load command to insert all the data in a CSV file to the mysql table. The load query sample is:
LOAD DATA LOCAL INFILE 'C:\\path\\to\\windows\\file.CSV'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, field2, field3, fieldx);
The data in the file has the following format:
FName || LName || num1 || num2 || num3|| num4 || num5 || date
Here all nums are of Float data type.
Here the date format of date in csv file is dd-MM-yyyy.
So when loading the complete file in DB I am storing dates as a varchar, because when I store them in a DATE datatype I get 0000-00-00.
Now after inserting data I have to work on dates but I am not able to get the sorted dates as they are stored as a Varchar.
Is there any way I can specify the default dateformat at the time of table creation. For example:
create table test (
mydates date(date : dd-mm-yyyy));
something like this.
Or could anyone suggest a different approach to tackle this issue?
Use str_to_date to convert the string into a date time object and use set to set the column's value manually. Lets say fieldx is your date field:
LOAD DATA LOCAL INFILE 'C:\\path\\to\\windows\\file.CSV'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, field2, field3, #fieldx)
SET fieldx = str_to_date(#fieldx, "%d-%m-%Y");
Have a look at the manual page for load data for more information; and adjust the format string using this table.
I suggest using the "string to date" function
STR_TO_DATE(table.datestring, '%m-%d-%Y')

How to convert Date on MySQL (variable error)

I have created this table on MySQL :
create table Apolo(
Date date,
Name varchar(50)
);
I have imported an excel file :
LOAD DATA LOCAL INFILE 'C:/Users/File.csv'
INTO TABLE Apolo
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 10 ROWS
(#Date, Name);
set Date=str_to_date(#Date,'%d/%m/%Y');
and I get the error :
Error Code: 1193. Unknown system variable 'Date'
If I do not put this line :
set Date=str_to_date(#Date,'%d/%m/%Y');
I do not get the error but if I try to use :
select count(*) from Apolo where Date='03/09/2015';
it does not work. So the format is not recognisable.
The date to insert into mysql database should be in the format
YYYY-MM-DD
Example: 2015-02-18
So change the date in csv file to the above specified format and try ..
Check your Date column values in database. By default date format in mysql database is Y-m-d
Do you have values in this format.
Take the semi-colon out of this line
(#Date, Name);
The set should be in the same command. Watch out for the fact that you have a field name that is a reserved word - you may need to escape it.

Mysql STR_TO_DATE incorrect datetime value

I've loaded some date from file to table and now i want to convert the string with date to a datetime format.
The string i 'datestring' column looks like this '12-16-2010 01:48:28', and if i run this query:
select STR_TO_DATE('12-16-2010 01:48:28', '%c-%e-%Y %T')
It returns proper datetime: 2010-12-16 01:48:28
But when i try to run this:
update database.`temptable`
SET datetimefile = (SELECT STR_TO_DATE(datestring, '%c-%e-%Y %T'))
I get those kind of errors:
Incorrect datetime value: ''12-16-2010 01:48:28'' for function str_to_date
Any ideas?
Take a close look at the error message:
Incorrect datetime value: ''12-16-2010 01:48:28''
^^ 2 single quotes ^^
Compare this to the normal error message:
mysql> SELECT STR_TO_DATE('foo', '%c-%e-%Y %T');
+-----------------------------------+
| STR_TO_DATE('foo', '%c-%e-%Y %T') |
+-----------------------------------+
| NULL |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+----------------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------------+
| Warning | 1411 | Incorrect datetime value: 'foo' for function str_to_date |
+---------+------+----------------------------------------------------------+
1 row in set (0.00 sec) ^ ^ just 1 single quote
Normally, the error message has a single set of single quotes. Yours has a double set, suggesting that you actually have a set of single quotes stored in your column data.
If this is the case, you can work around this by removing them where they exist:
SET datetimefile = (SELECT STR_TO_DATE(REPLACE(datestring,"'",''), '%c-%e-%Y %T'))
Using REPLACE() like this still would work even if not all of the rows contain the spurious quotes, since replace passes through the input value unchanged if the 'from_str' (2nd arg) doesn't occur.
From PHP SQL Request Correct:
LOAD DATA INFILE '.$filename."' INTO TABLE tablename (#var_DTime, `Product`, `Source`, `Cost`) SET `DTime` = str_to_date(#var_DTime,'%Y-%m-%dT%H:%i:%s')
Do not use: " " - 2010-12-31 01:48:28; - Don't work
Use "T" - 2010-12-31T01:48:28; - Work
I had a similar problem,
I wanted to use order by date syntax, but since my dates were in text format it returned the table unsorted.
I tried using
Alter table
Alter column `column name` date
but it gave the me same error you've got.
the only solution that I found by trial and error was to change the format of the date in the CSV file. I changed it to YYYY-MM-DD, then used "Alter table , alter column column name" to change the data type in the column
This error happened when you are trying to add a non-DateTime value inside a DateTime column.
For example, the DateTime format is YYYY-MM-DD HH:MM:SS. So in any case, if you try to add a date with any format other than the original format, it will throw this error.
In my case, I was using LOAD DATA INFILE to load a CSV file, to a specific table. One of the columns in the CSV file was in another date format, so I was supposed to change the format using str_to_date function using the below syntax:
LOAD DATA INFILE 'x.csv'
INTO TABLE orders FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(
timimg,
order_id,
order_details
)
set timing= STR_TO_DATE(lower(#timing), '%d-%b-%Y %H:%i:%s');
The above syntax throws the same error (Incorrect DateTime value for function str_to_date), So what was the issue?.
It was just adding the # for the timing column. # character means that you are telling MySQL to not load the data directly in the table for that column, however, make some changes to the value from CSV before inserting in the table.
So by adding the character # in the above query, It fixes the issue. The correct syntax is:
LOAD DATA INFILE 'x.csv'
INTO TABLE orders FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(
#timimg,
order_id,
order_details
)
set timing= STR_TO_DATE(lower(#timing), '%d-%b-%Y %H:%i:%s');
... SET datetimefile = STR_TO_DATE(datestring, '%c-%e-%Y-%T')
Note the lack of select around the str_to_date call. That select had no table reference, so the query failed with "unknown field datestring". That failure bubbled upwards and killed the entire overall query.

Load Data Infile Error with date

I am trying to load a .csv file that has 5 columns into a table that has the same corresponding columns plus a PK. Dates are data type DATE and all others are Varchar(), except PK.
Here is my load data Import:
LOAD DATA INFILE 'C:\Events_Upload.csv'
INTO TABLE db.events
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
Ignore 1 lines
(
Event_Name,
Event_Handle,
Create_Date,
Retire_Date,
Event_Desc
)
The error is :
Error Code: 1292. Incorrect date value: '' for column 'Retire_Date' at row 1
Row one in CSV looks like:
Time to Send Survey,SurvESend,2013-04-10,,Time for the system to send out the Esurveys
How can I make this field NULL and not blank for uploading from CSV?
There is nothing as null in CSV file. What you can do is may be provide some default value for the missing column and replace it with null by the query after insertion into the table.
Or you can have a look at skipping-empty-csv-objects
check this Export null to .csv and https://superuser.com/questions/390031/how-to-write-null-into-csv-from-excel-for-blank-fields

string to timestamp mysql Error #1411

I'm trying to convert timestamps on the fly when importing a csv file into mysql from string to datetime data type. But I am getting a #1411 - Incorrect datetime value: '2007-03-30 16:01:15' for function str_to_date error.
The SQL:
load data infile 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
into table test
fields terminated by ','
lines terminated by '\n'
(date, col1,col2,col3,col4)
SET
date = str_to_date(date,'%Y.%m.%d %H:%i:%s.%f');
All rows in the .csv are formated like this:
2007.03.30 16:01:15.901,117.53,117.55,35600000,43700000
I've applied
SELECT str_to_date(date,'%Y.%m.%d %H:%i:%s.%f') FROM test
to sample data that was already stored in mysql, it did work.
The target row date is set to DATETIME.
You need to go via a user variable. As the manual says:
The column list can contain either column names or user variables. With user variables, the SET clause enables you to perform transformations on their values before assigning the result to columns.
User variables in the SET clause can be used in several ways. The following example uses the first input column directly for the value of t1.column1, and assigns the second input column to a user variable that is subjected to a division operation before being used for the value of t1.column2:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, #var1)
SET column2 = #var1/100;
In your case:
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(#date, col1, col2, col3, col4)
SET date = STR_TO_DATE(#date, '%Y.%m.%d %H:%i:%s.%f');