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
Related
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 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')
Hi so I am trying to delete rows in mysql where the dates are equal to a start and end date. The problem is when I run this query:
DELETE FROM taddmapp.sensorsummaryall WHERE startDate = '2017-07-14T03:01:42-04:00' AND endDate = '2017-07-14T19:10:36-04:00';
i get an error saying "Error Code: 1292. Incorrect datetime value: '2017-07-14T03:01:42-04:00' for column 'startDate' at row 1".
The only thing is when i run this select query:
SELECT * FROM taddmapp.sensorsummaryall WHERE startDate = '2017-07-14T03:01:42-04:00' AND endDate = '2017-07-14T19:10:36-04:00';
with the same dates for start and end it returns me all the rows that have start and end date equal. Can someone explain why the SELECT works but the DELETE gives me an error? Thank you in advance for your time and effort.
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.
I am trying to query an SQL table using a time range via the statement below.
SELECT columnName FROM tableName WHERE time between 2013-05-03 22:47:02 and 2013-11-19 22:47:02
Where the column 'time' is of type TIMESTAMP, an example entry is
2013-09-07 15:00:00
I am getting the response that there is a syntax problem can anyone tell me what I'm doing wrong?
You need single quotes around the datetime values.