MySQL query not deleting rows using datetime - mysql

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.

Related

Compare User input

I have a set of date (start date & end date) in DB, how to validate for duplicate date set between user input and DB. (Mysql)
i have try a few method but it only compare either start or end date only. not both at the same time.
I think you are looking for the following logic.
Filter using between SQL command
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
use the following logic:
User registered new START DATE and END DATE
Search for START DATE using BETWEEN into DATABASE
Search for END DATE using BETWEEN into DATABASE
If those values exists into ranges you can show duplicated message.
Regards
C.

Inserting date from working subselect occurs in error

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

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 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.

mysql update query with select as criteria

UPDATE reservation SET flag = "1" WHERE ipAddress = (SELECT ipAddress FROM reservation WHERE endDate < CURRENT_TIMESTAMP);
Im trying to use this query for changing flag column of those entries in reservation table whose date has expired. The flag column is by default 0. so im trying to change expired once to 1 for my identification.
Im getting the following error.
ERROR 1093 (HY000): You can't specify target table 'reservation' for updatein FROM clause
Can someone suggest a solution to this problem..
if you're using the same table in the subselect, you can probably omit the subselect altogether.
why dont you use the following instead
UPDATE reservation
SET flag = "1"
WHERE endDate < CURRENT_TIMESTAMP
You do not need the inner SELECT. You just need to update the flag status based on the endDate
UPDATE reservation
SET flag = "1"
WHERE endDate < CURRENT_TIMESTAMP