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'
Related
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
I have a table in which there is a column called "DATE" which contains dates in the format "23-Nov-2017" as datatype VARCHAR. I'm trying to convert this VARCHAR column and store it in a new column called "NEWDATE" of datatype DATE.
I have created the new column "NEWDATE" of type DATE and I am trying to use the STR_TO_DATE() function to perform the conversion. However, I can't get it to work for some reason.
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%m-%Y');
The NEWDATE column is not updated with any values after the statement. I guess this means that the statement does not execute. What am I doing wrong?
EDIT: I have also tried STR_TO_DATE(DATE,'%d-%b-%Y'). However there is still no change to the values in the NEWDATE column
Your format '%d-%m-%Y' does not match your actual date string "23-Nov-2017"
The %m is for numeric month and you have an abbreviated text month
Use %b for 3 char month values like this:
STR_TO_DATE(DATE,'%d-%b-%Y')
EDIT: WorkBench issue
That is just a Workbench config setting to stop you accidentally issuing a HUGE update. You can either turn that setting OFF or frig it a bit by giving it a WHERE clause that will allow it to run like below. Below assumes this table has an id column
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id<10000000;
Or
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id>0;
I am running an append query that selects records from an existing table in the database. All the fields in the receiving table are defined as text. Since this table will receive new records on a regular basis, I want to add a file date in text form to the record when it's written so I can identify when each record was added. I don't need the file date to be an actual date, and it will not be the date I'm actually running the query. The field is defined as text, and the query prompts for the date.
Query from comment:
INSERT INTO tblMaster_Vendor ( Vendor_ID, FileDate )
SELECT DISTINCT FULL_EXTRACT.Vendor_ID, [File Date] AS Expr1
FROM FULL_EXTRACT
WHERE FULL_EXTRACT.Vendor_Type = "84"
Every time, I get a 'data type conversion error', even though I'm adding text (like 20171106) to a text field.
Here's what I've tried:
- changed the field name and prompt from 'File Date' to 'Entered' in case there was a reserved word issue
- Entered straight text into the parameter query box (aaaabbcc)
- Changed the table field type to date/time and entered the date as 11/10/2017
- Entered the text in the parameter box with quotes ('aaaabbcc')
If I don't try to enter the file date as part of the append query, I can run an update query after the append and update the field to my desired value (20171106).
Has anyone seen this behavior before?
Have you tried using the Cstr function? e.g., something of this structure?
INSERT INTO Table2 ( ID, fileupdated ) SELECT Table1.ID, CStr(Now()) AS Expr1 FROM Table1 WHERE (((Table1.ID)>2));
CSV date format is DD/MM/YYYY like this 16/11/2016. All the date become 0000-00-00 in MySQL. How to solve this differences?
As of now I can think of two solutions to you problem:
Create a additional VARCHAR field to insert those values (like 16/11/2016), and create a TRIGGER on INSERT to update the date field by converting the string date to 'YYYY-mm-dd' type.
These links may help
https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
http://www.w3resource.com/mysql/date-and-time-functions/mysql-str_to_date-function.php
In this step also, Create a additional VARCHAR field to insert those values (like 16/11/2016) and after import is done run a UPDATE query to update your date field using SET dateField = convertDate(dateFromSheet)
I am trying to further a question I asked yesterday where I wanted to know how to query a date in a different format. But now I am trying to do an insert using this method (see below) however I can't get it to work. I have checked the manual but it is not beginner friendly!
INSERT INTO custorder VALUES ('Kevin','yes'), STR_TO_DATE('1-01-2012', '%d-%m-%Y');
Put the date in single quotes and move the parenthesis (after the 'yes') to the end:
INSERT INTO custorder
VALUES ('Kevin', 'yes' , STR_TO_DATE('1-01-2012', '%d-%m-%Y') ) ;
^ ^
---parenthesis removed--| and added here ------|
But you can always use dates without STR_TO_DATE() function, just use the (Y-m-d) '20120101' or '2012-01-01' format. Check the MySQL docs: Date and Time Literals
INSERT INTO custorder
VALUES ('Kevin', 'yes', '2012-01-01') ;
Looks like you've not encapsulated your string properly. Try this:
INSERT INTO custorder VALUES ('Kevin','yes'), STR_TO_DATE('1-01-2012', '%d-%m-%Y');
Alternatively, you can do the following but it is not recommended. Make sure that you use STR_TO-DATE it is because when you are developing web applications you have to explicitly convert String to Date which is annoying. Use first One.
INSERT INTO custorder VALUES ('Kevin','yes'), '2012-01-01';
I'm not confident that the above SQL is valid, however, and you may want to move the date part into the brackets. If you can provide the exact error you're getting, I might be able to more directly help with the issue.
The date format for mysql insert query is YYYY-MM-DD
example:
INSERT INTO table_name (date_column) VALUE ('YYYY-MM-DD');
An add-on to the previous answers since I came across this concern:
If you really want to insert something like 24-May-2005 to your DATE column, you could do something like this:
INSERT INTO someTable(Empid,Date_Joined)
VALUES
('S710',STR_TO_DATE('24-May-2005', '%d-%M-%Y'));
In the above query please note that if it's May(ie: the month in letters) the format should be %M.
NOTE: I tried this with the latest MySQL version 8.0 and it works!
When using a string-typed variable in PHP containing a date, the variable must be enclosed in single quotes:
$NEW_DATE = '1997-07-15';
$sql = "INSERT INTO tbl (NEW_DATE, ...) VALUES ('$NEW_DATE', ...)";