I am trying to insert VARCHARs strings which are not a valid date format into another table where they are also stored as a VARCHAR. I'm trying to use STR_TO_DATE to accomplish this, which is supposed to return NULL if a string's format is invalid:
INSERT INTO DateErrors
SELECT `row_id`,`date_string`
FROM `source_table`
WHERE STR_TO_DATE(`date_string`,'%d-%m-%Y') IS NULL
Which throws an error for strings with an invalid date format:
Error Code: 1411. Incorrect datetime value: 'some random string that isnt a date' for function str_to_date. Instead of throwing this error, I want these string values inserted into DateErrors.
However, running just the SELECT query returns the rows where date_string is invalid without throwing an error.
You are specifying wrong format for your date string in STR_TO_DATE
STR_TO_DATE(`date_string`,'%d-%m-%Y')
instead, it should be
STR_TO_DATE(`date_string`,'%Y-%m-%d')
Related
I code in the SQL, but I want to change the date format from default of My SQL to different format "DD/MM/YYYY" to use this format to code, but I recieved an error.
More about version of SQL:
SQL: My SQL Workbench 8.0.30 build 2054668
Window 11 Pro
Language: English
So, how can I change the date format or what is the true type of "dmy"? Please help me.
This is code that I used:
set dateformat dmy
And error is:
"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 'dmy' at line 1"
At the moment, we don't really know the reason why you want to change the date format, but I have a couple of assumption:
You want to insert date data from front end into the table but couldn't do so due to the mismatched date format.
You want the end output to show the date format of DD/MM/YYYY instead.
Although, it may look like you need to change the table column date format, there's a way to avoid that operation entirely. However, since you've mentioned changing from YYYY/MM/DD in your title, I'm not sure if your date column is actually DATE datatype because the default MySQL should be YYYY-MM-DD. Nonetheless, I'll address the matter in this answer altogether.
Modify the date format in query and leave the table date column datatype as it is:
From DD\MM\YYYY to MySQL DATE datatype format YYYY-MM-DD
... STR_TO_DATE(data, '%d/%m/%Y')
.. from YYYY-MM-DD to DD/MM/YYYY
... DATE_FORMAT(data, '%d/%m/%Y')
You can use any of that anywhere in a query; whether in SELECT or WHERE.
If "in query" is not what you want and you still want to update the table:
Well, you have two options here:
Directly modify the column datatype then update the value:
ALTER TABLE mytable MODIFY COLUMN date_col VARCHAR(255);
UPDATE mytable SET date_col =DATE_FORMAT(date_col , '%d/%m/%Y');
Or you can add another column, populate the desired date format there and keep the original date column as it is:
ALTER TABLE mytable ADD COLUMN my_date VARCHAR(255);
UPDATE mytable SET my_date =DATE_FORMAT(date_col , '%d/%m/%Y');
this way you have the option to directly use MySQL date functions on the default MySQL date column without the hassle of converting your desired date format into the default before you can use date functions. What I'm saying is something like this:
DAY(mysql_default_dateformat)
is similar to
DAY(STR_TO_DATE(your_dateformat, '%d/%m/%Y'))
which means that you can use DAY() (date function) on the default date format directly without the need to convert what is not default first.
Here's demo fiddle examples
I have this column called as "orderdate" that has varchar data type. I want to convert it to date using this
update january_2019 set orderdate = str_to_date(orderdate,'%m/%d/%y');
but eventually it generates error
Error Code: 1411. Incorrect datetime value: 'Order' for function str_to_date
Any help?
Context:
CREATE TABLE dates (
date DATE
);
I have following select query which returns 2000-01-01, because LAST_DAY function returns null because of invalid date.
Edit: I know that 2012-02-31 is incorrect date. That's the point of this question.
SELECT IFNULL(LAST_DAY('2012-02-31'), '2000-01-01'); // returns 2000-01-01
However when I try to insert this result into the table it returns an error:
Query Error: Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: '2012-02-31'
INSERT INTO dates (date)
SELECT IFNULL(LAST_DAY('2012-02-31'), '2000-01-01');
Why does it happen like that? What's the explanation?
DB Fiddle
i have a column with the datatype DATETIME and im trying to get it to automatically insert the current date and time. i tried setting the value to NOW() but it gave the following error
ERROR 1292: 1292: Incorrect datetime value: 'NOW()' for column 'date' at row 1
does anyone know how this is accomplished? im using mysql workbench
NOW() should return the datetime correctly which should match the field type. As I mentioned in the comment, you might be inserting the value "NOW()" as a literal string, which isn't a valid datetime format. Make sure you don't have quotes around NOW().
I have a CSV excel file that I want to insert in my MySQL database...but it's giving me an error code on my date.
Dates-Excel :
Error-code-MySQL :
I have tried to set my date in MySQL to datetime but that doesn't work...
you can use str_to_date() function
select str_to_date('2-3-2017','%d-%m-%YYYY')