How to convert date and update a joined table - mysql

I am trying to update a column in table, based on data (that is constantly updated) in a temporary table (via csv imports). The date in the temporary table is VARCHAR ("2/20/2014 10:29:25 AM" format) and the date in the table I want to update is in DATETIME type.
I'm not sure how to properly join these two tables to update the dates in the permanent table based on the current date showing in the temp table...using product_sku (in both tables) as the joining field.
Here is what I have tried, but keep getting SQL error (' #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM...)
Update t0
set t0.product_available_date =
(SELECT STR_TO_DATE( `t1.product_available_date`,
'%c/%e/%Y %H:%i' ))
FROM cpg5443_virtuemart_products as t0
join cpg5443_virtuemart_products_temp as t1
on t0.product_sku = t1.product_sku
Where t0.product_sku='002-765-AS'
Do you see any glaring mistakes and can you advise how I can correct? In the end, I will want the where statement to say "where t0.product_sku=t1.product_sku" with hopes this will update this column for every record based on the date in the temp table. For now, I'm just testing the update with one product_sku.

Looks like you are missing parentheses after '002-765-AS'. Have you posted all the code? Also you have an extra parentheses after the date format.
Try this:
update cpg5443_virtuemart_products as t0
inner join cpg5443_virtuemart_products_temp t1
on t0.product_sku = t1.product_sku
set t0.product_available_date = str_to_date (t1.product_available_date,
'%c/%e/%Y %H:%i:%s')
where t0.product_sku = '002-765-AS'

Related

How to update empty time in datetime MySQL

We are storing datetime in a column on our MySQL database, formatted in TEXT, but when our datetime is supposed to look like below:
'xxxx-xx-xx 00:00:00'
The time is deleted or not show on our datetime, and therefore our datetime, at that specific time, only contains the date:
'xxxx-xx-xx'
What we want is first of all to figure out why this is occurring, but for now we need to edit every row, and make sure the datetime is also showing the time. We have tried to change the impacted rows by using this query:
UPDATE table SET TIME(col_datetime) = '00:00:00' WHERE LENGTH(TIME(col_datetime)) = 0;
Above query should update the time on the datetime for col_datetime, where length of time is 0. Unfortunately, we receive an error, and we can't run the query. This is the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(time_start) = '00:00:00' WHERE LENGTH(TIME(time_start)) = 0' at line 2
How can we change time on our datetime, where time is not shown?
Don't store dates as strings. Instead, you want to use the datetime datatype: it has a time part, that defaults to 00:00:00 when not specified.
Here is a small conversion script for that purpose:
alter table mytable add col_datetime_new datetime;
update mytable set col_datetime_new = col_datetime;
alter table mytable drop col_datetime;
alter table mytable change column col_datetime_new col_datetime datetime;
This leverages the fact that you are using format YYYY-MM-DD in your string dates, so conversion to datetime is seemless.

MySql update query not working-generic error

I get a generic error message, and have no idea what the problem is with the query. What do I do to fix this?
Query explanation: there are two tables, Invoice, and temp. I need to take zip codes from temp table and push them to the Invoice table, based on the invoice number.
START TRANSACTION
UPDATE
Invoice
SET
Invoice.zip_code = (SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number)
WHERE
Invoice.invoice_date >= '2017-08-01'
ROLLBACK
this is the error:
Error Code: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'UPDATE Invoice SET Invoice.zip_code = (SELECT
zip_code FROM temp WHERE temp' at line 3
Add semicolon after each command.
START TRANSACTION;
UPDATE
Invoice
SET
Invoice.zip_code = (SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number)
WHERE
Invoice.invoice_date >= '2017-08-01';
ROLLBACK;
Sounds like #Daniel Blais is right. One thing you can do to troubleshoot is to break the query down and run each part individually. That will help you find out what's wrong.
SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number;
One other question: Why are you rolling back after the table is updated? Don't you want to commit?

MySQL STR_TO_DATE NULL on error

I'm currently migrating a table with dates in VARCHAR columns to a new table with DATE columns. I managed to sanitize the string values in the old table to the format "YYYY-MM-DD" but when I try to perform the insert I got an error with the date "2006-04-31" because that April only had 30 days (was a typo when it was registered),
My question is: how can I set to NULL the column when the date is invalid without getting an error? My SQL is the following:
INSERT INTO newFancyTable (created_at)
SELECT str_to_date(created, '%Y-%m-%d') FROM oldCrappyTable;
And the error is the following:
Error Code: 1292. Incorrect date value: '2006-04-31' for column 'created_at' at row 1
Thanks
UPDATE
I also tried using the following approach:
INSERT INTO newFancyTable (created_at)
SELECT CAST(created AS DATE) FROM oldCrappyTable;
With the same error, and trying to update the oldCrappyTable would return the same:
UPDATE oldCrappyTable SET created = CAST(created AS DATE);
Both return:
Error Code: 1292. Incorrect datetime value: '2006-04-31'
UPDATE 2
At last, I used multiple CASEs to isolate that invalid dates, in sum they were only 5 of them,
Nevertheless, the issue can be reproduced by doing:
CREATE TABLE dates_temp (
test_date DATE DEFAULT NULL
) ENGINE=MEMORY;
INSERT INTO dates_temp
SELECT STR_TO_DATE("2006-04-31", '%Y-%m-%d');
DROP TABLE dates_temp;
A possible workaround is to turn off strict mode, either for the whole server, for a particular session, or for just a few statements. For example:
set #old_sql_mode = ##sql_mode;
set sql_mode = '';
-- Run some statements which may result in error
set sql_mode = #old_sql_mode;
Additional Info
MySQL Documentation
In addition to #EduardoDennis answer use NULLIF to filter out zero dates:
INSERT INTO newFancyTable (created_at)
SELECT nullif(str_to_date(created, '%Y-%m-%d'), from_days(0)) FROM oldCrappyTable;
See my full answer here.

Issue of updating existing table with results of select statement for time zone conversion

I have a table called table1 with three columns, one of which is Date_Of_Call which is of datetime type with the data in PDT. I basically need to convert the data from PDT to UTC and put the UTC converted dates into a new column in the existing table. I added a new column with:
alter table table1 ADD Date_Of_Call_UTC DATETIME;
I am able to get the proper time conversion with this select statement:
select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1;
The issue I am having is trying to use an update command to take the results of the select statement and put them in the new Date_Of_Call_UTC column. Any thoughts of how to do this?
I tried the below statement and a few variations but can't quite figure out what I need to do:
update table1 set table1.Date_Of_Call_UTC = (select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1);
Any assistance is appreciated!
this one should work:
update table1
set table1.Date_Of_Call_UTC = CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00');
NOTE: dates are usually stored already as UTC in mysql, but during output they can be displayed with offset applied, read about it: http://dev.mysql.com/doc/refman/5.0/en/datetime.html and http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

ALTER TABLE ORDER BY str_to_date and time_to_sec

I am trying to re-order a table after importing a file but am getting a mysql syntax error on this query:
ALTER TABLE tablename ORDER BY str_to_date(date, '%m/%d/%Y'), time_to_sec(time) ASC
Can anyone see anything wrong with this? this is the error I am getting:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(date, '%m/%d/%Y'), time_to_sec(time) ASC' at line 1
According to mysql manual, ALTER TABLE ORDER BY expects a col_name, but you are trying to order it using a function.
I suggest create 2 new columns, populate them with function results, and then order by them.
You might have to use a temp table for this since you're ordering by a function.
CREATE TABLE temp_tablename AS SELECT * FROM tablename;
TRUNCATE tablename;
INSERT INTO tablename SELECT * FROM temp_tablename;
DROP temp_tablename;
You could make the first statement a CREATE TEMPORARY TABLE, but if you lose your session you lose your data.
Did you try
ALTER TABLE tablename ORDER BY date, time ASC
I mention this because it might give you the order you need.
Otherwise you'll need to either do as German Rumm suggested, add columns with the correct datatype, or do your ordering when you do your SQL.
I think I resolved this issue. I was storing imported data in a temp table before moving it onto it's permanent table and needed to sort the data in the temp table first before inserting into the new table. so what I do instead is insert into the new table via a select statement which has the order by statement.
You are using an older version of mysql, STR_TO_DATE is only availabe from version 4.1.1 of MySQL.
Update MySQL version.