Mysql Incorrect date format while inserting - mysql

When was trying to insert some data to a table, I encountered this error. Incorrect datetime value: 'dweadwaed' for function str_to_date.
I was using INSERT INTO .. SELECT method to do it. The SELECT query works fine, but when I combined it with the insert query, the error happened.
SELECT id,'INVALID_DATE' FROM invoice WHERE STR_TO_DATE(paid_date, '%Y-%m-%d') IS NULL
This query returns the row 1,3.
And these 3 rows should have been inserted into the validation table using the below query. But it is showing error.
INSERT INTO validation(invoice_id,validation_message)
SELECT id,'INVALID_DATE' FROM invoice WHERE STR_TO_DATE(paid_date, '%Y-%m-%d') IS NULL
Basically the select query is same, and I am not trying to insert any date field to the DB too.
Can anyone explain the reason for this behaviour of Mysql.
I have added a SQL Fiddle for the 2 tables
We are trying to migrate data from an external application. So we cannot change the existing DB structure. Instead we are trying to identify and correct their invalid dates.

paid_date is Varchar(50)
you have inserted dweadwaed' for paid_date
Select list contains this value for paid_date so the error

Related

Insert a date in MySQL only if it is valid

I have a table with date values stored as strings, like '2012-01-15'. Some of them are invalid, like '2012-04-31'. I would like to insert the valid dates into a DATE type column in another table, and default the day to 1 if it is too large for the month.
DAYNAME seems to be the only function in MySQL that will check whether a date is valid. However, it issues a warning for an invalid date (in addition to returning NULL), which upgrades to an error in an INSERT or UPDATE statement.
So I'd like to do something like
INSERT INTO date_tbl (date_value)
SELECT IF(DAYNAME(date_string) IS NOT NULL, date_string, CONCAT(LEFT(date_string, 8), '1')
FROM date_string_table;
This fails with Data truncation: Incorrect datetime value: '2010-04-31' even though I am not actually inserting invalid data.
The problem with using INSERT IGNORE is running the risk of actually inserting invalid data, which I would really like to avoid.
EDIT Oct 5:
This problem can be reproduced without creating the intermediate table simply as
CREATE TABLE date_tbl (
date_val DATETIME
);
INSERT INTO date_tbl (date_val)
SELECT IF(DAYNAME('2012-04-31') IS NOT NULL, '2012-04-31', NULL);
I would like the above INSERT to insert NULL for that invalid date, instead of failing.
You can compare days of the proper date and last day for that month with LAST_DAY and STR_TO_DATE.
So your query would be:
INSERT INTO date_tbl (date_val)
SELECT IF(DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d')) > DAY(LAST_DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d'))), NULL,'2012-02-30');
DB Fiddle
A workaround is to use INSERT IGNORE and then validate after the fact:
SELECT date_value
FROM date_tbl
WHERE DAYNAME(date_value) IS NULL;
Should return zero rows.

MySQL INSERT INTO statement generating "Error Code: 1136. Column count doesn't match value count at row"

I am trying to create a new table from an old table so I can remove some duplicates and implement a composite primary key. I have created the new table from the old one using LIKE, so the columns should be identical.
Why does this not work?
INSERT INTO PricesNEWtest (Global_MarketCap,pkey)
VALUES ((SELECT max(Global_MarketCap), pkey
FROM Prices GROUP BY pkey));
Error generated:
Error Code: 1136. Column count doesn't match value count at row 1
The example above only has two rows so it's more legible, but in reality the tables contain 15 columns, this is the full INSERT INTO statement, which generates the same errror:
INSERT INTO PricesNEWtest (Global_MarketCap,Global_Volume24h,BTC_Dominance,Rank,Name,
Symbol,ChangePerc24h,Price,BTC_Price,MarketCap,Volume24h,DateTime,Date,pkDummy,pkey)
VALUES ((SELECT max(Global_MarketCap), max(Global_Volume24h), max(BTC_Dominance), max(Rank), max(Name),
max(Symbol), max(ChangePerc24h), max(Price), max(BTC_Price), max(MarketCap), max(Volume24h),
max(DateTime), max(Date), max(pkDummy), pkey
FROM Prices GROUP BY pkey));
I added the double brackets for VALUES because without it I get error code 1064, but I don't fully understand why the double brackets are necessary. I am grouping by the pkey field (which currently has some duplicates I want to delete), which means I need to summarize the rest of the fields. The SELECT statement works fine on its own as you can see from the screenshot.
Is there another way to do this that I could try? Or is there an easier way to remove the duplicates directly from the original table?
I am using MySQL 5.7.14
Any help would be appreciated!
You just have the wrong syntax to INSERT with a SELECT statement:
INSERT INTO PricesNEWtest (Global_MarketCap, pkey)
SELECT max(Global_MarketCap), pkey
FROM Prices
GROUP BY pkey

insert data from one table to another - Not Working - possible bug?

I'm trying to insert data from one table to another, but I keep getting a weird SQL error. The following is my query along with the error.
insert into hs.hs (`field1`,`field2`,`field3`) select cid,sid,'1' from `tmp1`;
now the error
1416 - Cannot get geometry object from data you send to the GEOMETRY field.
I don't understand why I'm getting a geometry error there is no geometry involved at all.
By the way my MySQL version is:
SELECT VERSION(); Result 5.7.16-log
I'm pretty sure I'm doing everything right; Can some one please tell me what I'm doing wrong?
Could this be a bug?
Thanks I'm looking forward to some help.
You can try this -
insert into hs (field1,field2,field3) select cid , sid, '1' from tmp1;
If this wont work then try please check the columns contained in both the tables. If your "hs" table contains more fields then either you have to assign them values by fetching from other table or give them some default values.
insert into hs (field1,field2,field3) select cid , sid, '1' from tmp1;
Below is the reason why the above query didn't worked
INSERT INTO SELECT requires that data types in source and target tables match
INSERT statement should contain all columns or they should have some default value assigned

Unable to copy data from one table to a new table in MySQL due to a String - Date type clash

I've a date column in table1 filled with character data that looks as follows:
InDate:
2022015
2012015
2122014
2112014
2102014
2092014
I've converted that data to following format:
InDate:
02-02-2015
02-01-2015
02-12-2014
02-11-2014
02-10-2014
02-09-2014
by using the following command:
update table1
set InDate = DATE_FORMAT(str_to_date(InDate, '%d-%m-%Y'), '%d-%m-%Y');
But obviously this only changed the look of the string data in the InDate column. Now, this still didn't suffice my situation and I needed the column itself to be in Date type.
So, I created a new empty table called table2 with the same structure as table1 (except for InDate column, which I now declared as Date type) and tried copying the old data (which is having InDate in my required format (02-02-2015)).
INSERT INTO table2
SELECT *
FROM table1;
But I'm getting the following error message:
Error Code: 1292. Incorrect date value: '02-02-2015' for column 'InDate' at row 1
I need this column to be in Date type with the data intact. This table has millions of records that I need to work with. I really need to get around this error.
Please help!
This code block maybe help use :
Not update table1.
insert into table2
(indate)
select date_format(str_to_date(indate
,'%d-%m-%Y')
,'%d-%m-%Y')
from table1;
Please try this.
INSERT INTO table2(`InDate`)
SELECT CONCAT('\'',InDate,'\'')
FROM table1;
Hope this helps.

Trying to convert values in SQL to Unix Timestamp

I have a Wordpress table that has a bunch of values currently in date format MM/DD/YYYY. This query successfully shows me the Unix Timestamps for them:
SELECT UNIX_TIMESTAMP(STR_TO_DATE(`meta_value`,'%m/%d/%Y'))
FROM `wp_6222_postmeta`
WHERE `meta_key`="wpcf-start-date"
But I'm struggling to figure out how to write those values back into the field properly. I tried this query and it didn't work:
UPDATE 'wp_6222_postmeta'
SET 'meta_value' = UNIX_TIMESTAMP(STR_TO_DATE(`meta_value`,'%m/%d/%Y'))
WHERE 'meta_key' = 'wpcf-start-date'
Pasting answer here since I couldn't answer it before because my account was too new.
Ok the syntax error ended up being the addition of quotation marks for the column fields which UPDATE must not like. Removing those gave me NULL values but the query ran. I then found I had to wrap the timestamp conversion in a SELECT function. Here's the code I ran that successfully converted all the fields:
UPDATE wp_6222_postmeta
SET meta_value = (SELECT UNIX_TIMESTAMP(STR_TO_DATE(meta_value`,'%m/%d/%Y')))
WHERE meta_key='wpcf-end-date'