Mysql STR_TO_DATE incorrect datetime value - mysql

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.

Related

Dates not importing correctly

I know similar questions have been posted before, but when I try to follow similar approaches as per the suggestions in the comments, it simply does not help. My query is the following:
LOAD DATA INFILE 'File.txt'
IGNORE
INTO TABLE table_name
FIELDS TERMINATED BY '^~'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(RUN_DATE, PROC_DT, STL_DT, TRD_DT)
SET RUN_DATE = STR_TO_DATE(RUN_DATE, '%d-%b-%y');
The records in the file look something like this:
RUN_DATE^~PROC_DT^~STL_DT^~TRD_DT
21-DEC-20^~23-DEC-20^~23-DEC-20^~21-DEC-20
The dates that get loaded are all populated as '0000-00-00 00:00:00' which I know are the default values when there is a datatype error and IGNORE is used. From what I found online, the issue has to do with the in-file date not being in yyyy-mm-dd format which is the default for mySQL, but the '%d-%b-%y' in the STR_TO_DATE function should help alleviate this issue since
%d: Day of the month as a numeric value (01 to 31) -
%b: Abbreviated month name (Jan to Dec) -
%y: Year as a numeric, 2-digit value
Why is this not helping? I also tried making the months lower case using LOWER() thinking maybe the abbreviated months needed to be all lower case, but this produces the same result. What am I missing here?
To read from the file but store a modified value, you need to use variables:
LOAD DATA INFILE 'File.txt'
IGNORE
INTO TABLE table_name
FIELDS TERMINATED BY '^~'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(#RUN_DATE, #PROC_DT, #STL_DT, #TRD_DT)
SET RUN_DATE = STR_TO_DATE(#RUN_DATE, '%d-%b-%y'),
PROC_DT = STR_TO_DATE(#PROC_DT, '%d-%b-%y'),
STL_DT = STR_TO_DATE(#STL_DT, '%d-%b-%y'),
TRD_DT = STR_TO_DATE(#TRD_DT, '%d-%b-%y');

load/import fails in mysql due to date format

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

MySQL:values are not correctly imported from yyyymmdd to date variable, using str_to_date

Here is my code:
CREATE TABLE A
(`ID` INT NULL,
`DATE` DATE NULL,
`NUM` INT NULL
);
LOAD DATA LOCAL INFILE "fakepath/file.csv"
INTO TABLE A
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ID,DATE,NUM)
SET
DATE = str_to_date(#DATE, '%Y%m%d');
The original data in csv file is like this-- 20160101,20160102,20160103 (the date is different). After I execute the code, all the date in the DATE column become one day value such as 2016-01-02 in table A.
Why do this happen? I have other table which used the same code(different column name)
How can I fix it? Thank you!
You have to tell MySQL to load the from-csv data value into a variable first:
LOAD DATA LOCAL INFILE "fakepath/file.csv"
[..snip..]
IGNORE 1 LINES
(ID,DATE,NUM)
^---table field, **NOT** a variable
SET
DATE = str_to_date(#DATE, '%Y%m%d');
^---variable never gets populated
Try
(ID, #DATE, NUM)
^--note this
instead. That'll load the id/num values directly into the table, but puts your date value into the variable, which you can use afterwards in the SET portion of the query.
The fact that you actually get a date value put into the table with a proper date format indicates that somwhere else, in previous code, you did set a #DATE variable, and it's simply being re-used in this query. But since you don't CHANGE that variable's value in this query, you end up using the SAME date value for all records.

Modify column before inserting XML value to MySQL table

I'm trying to import a XML file into a MySQL Table. In the XML file there is a timestamp in <CurrentTime> in the following format:
2016-01-26T09:52:19.3420655+01:00
This timstamp should go into the corresponding DATETIME CurrentTime column in my Table. So I did the following
LOAD XML INFILE 'xxx.xml'
INTO TABLE test.events
ROWS IDENTIFIED BY '<Event>'
SET CurrentTime = str_to_date(CurrentTime, '%Y-%m-%dT%H:%i:%s.%f');
But it quits with the error
Error Code: 1292. Incorrect datetime value: '2016-01-25T16:22:24.1840792+01:00' for column 'CurrentTime' at row 1
So it seems it doesn't convert the string at all. Why?
I think that error is thrown when the string value from the file is loaded directly to the column. The error is thrown before you get to the SET clause.
Here's an abbreviated example of how to use user-defined variables to pass the value of a field down to the SET, bypassing the assignment to the column.
Note that the columns _row and account_number are populated directly from the first two fields in the file. The later fields in the file are assigned to user-defined variables (identifiers beginning with #.
The SET clause evaluates the user-defined variables, and assigns the result of the expression to the actual column in the table.
In this example, the "dates" were formatted YYYYMMDD. I used the STR_TO_DATE() function to have that string converted to a proper DATE.
I abbreviated this sample somewhat, but it demonstrates the approach of reading field values into user-defined variables.
CREATE TABLE _import_water
(`_row` INT
,`account_number` VARCHAR(255)
,`total_due` DECIMAL(18,2)
,`end_date` DATE
,`start_date` DATE
,`ccf` DECIMAL(18,4)
)
LOAD DATA LOCAL INFILE '//server/share$/users/me/mydir/myfile.csv'
INTO TABLE _import_water
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(_row
,account_number
,#total_due
,#end_date
,#start_date
,#ccf
)
SET `total_due` = NULLIF(#total_due,'')
, `end_date` = STR_TO_DATE(#end_date,'%Y%m%d')
, `start_date` = STR_TO_DATE(#start_date,'%Y%m%d')
, `ccf` = NULLIF(#ccf,'')
Also, it doesn't look like there's any problem with your STR_TO_DATE, it seems to evaluate just fine.
testing...
SELECT STR_TO_DATE('2016-01-25T16:22:24.1840792+01:00','%Y-%m-%dT%H:%i:%s.%f') AS mydatetime
returns:
mydatetime
--------------------------
2016-01-25 16:22:24.184079

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.