mysql 0000-00-00 00:00:00 convert to null - mysql

In mysql Timestamp, I want to convert 0000-00-00 00:00:00 to null.
first I use, set sql_safe_updates = 0;
second I do, UPDATE TABLE SET FILED = NULL WHERE FILED = '0000-00-00 00:00:00';
but it also converts the current time like this: 2018-07-31 13:46:00
what's the problem?

First, change your field definition to allow NULL values. Use the following:
ALTER TABLE <tablename> MODIFY <columnname> TIMESTAMP NULL DEFAULT NULL;

Related

how to update datetime value from i.e 0000-00-00 00:00:00 value to null

Dear sir have a table in which I take a column updatetime with datetime datatype and also it is null by default. Now I want to replace some values from 0000-00-00 00:00:0 to null when I try to execute update query in phpmyadmin it give me enter image description here.Please help me how I update this column.`
UPDATE tbl_table_time
set update_datetime = NULL
WHERE update_datetime = '0000-00-00 00:00:00'
ID Updatetime (Datatype datetime)
1 0000-00-00 00:00:00
2 2020-12-03 16:19:07
3 0000-00-00 00:00:00
4 2020-12-03 16:19:07
5 0000-00-00 00:00:00
6 0000-00-00 00:00:00
7 0000-00-00 00:00:00
The problem is not the SET update_datetime = NULL but the WHERE update_datetime = '0000-00-00 00:00:00' of your query. The error indicates, that 0000-00-00 00:00:00 is not a valid Datetime, which is correct, as the lowest possible Datetime is 1000-01-01 00:00:00. Because of this MySQL does not want to compare the Datetime values. To overcome this, it's easiest, to convert the Datetime to an actual string for the comparison. Here is an example query:
UPDATE tbl_table_time SET update_datetime = NULL WHERE CAST(update_datetime AS CHAR(20)) = '0000-00-00 00:00:00'
Keep in mind that this is not very efficient and could take a while on large databases. And as 0000-00-00 00:00:00 is a faulty state, it should be avoided and you should check, how it ended up in the database anyway. MySQL is converting invalid Datetimes into this zero-state automatically, but this can also be disabled. For more details see https://dev.mysql.com/doc/refman/8.0/en/datetime.html

How to set timestamp back to 0000-00-00 00:00:00?

I have a MySQL table with a timestamp field
By default I can see that this field is set to 0000-00-00 00:00:00
I have some code that updates this field to a proper date. However I'd like to convert this field in some rows back to 0000-00-00 00:00:00
I tried something like: UPDATE mytable SET myfield='0000-00-00 00:00:00' WHERE id=72;
But that gives an error: Incorrect datetime value: '0000-00-00 00:00:00' for column 'myfield' at row 1
How can I get around this?
If myfield column is of datetime datatype, you can use this to set the time to 0000-00-00 00:00:00:
UPDATE mytable SET myfield = 0 WHERE id = 72;

Timestamp gets updated with zero

I need to update a column with datatype as timestamp. I run the following query:
update job_info set ProcessStartTime = UNIX_TIMESTAMP(CURDATE()) where JobID=4;
But it updates with a value : 0000-00-00 00:00:00 What could be the reason for this? Is the query incorrect?
Don't use UNIX_TIMESTAMP because MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds or your column type is datetime
update job_info set ProcessStartTime =CURDATE() where JobID=4;
or use NOW()
update job_info set ProcessStartTime =NOW() where JobID=4;

Find and replace data in a MySql database

I have two columns in a MySQL database, both has data/time
I have many records, that has actual date/time in the column sent_at like 2012-05-04 16:07:14
But the many records in the column date_recieved_by_proxy has null value like 00/00/0000 00:00
How can I give a sql command to replace date_recieved_by_proxy = 00/00/0000 00:00 records with the date of sent_at field?
update your_table
set date_recieved_by_proxy = sent_at
where date_recieved_by_proxy = '0000-00-00 00:00:00'
Alternatively you can try
where date_recieved_by_proxy is null
depending on the data type of your date_recieved_by_proxy column and if it can be null

0000-00-00 00:00:00 entered as MySQL DateTime

Below is the CREATE TABLE statement used to create my table:
CREATE TABLE IF NOT EXISTS `data_received` (
`id` int(10) unsigned NOT NULL,
`edit_time` datetime NOT NULL}
Below is how data is saved in table if 'edit_time' value is not provided:
id edit_time
1 0000-00-00 00:00:00
Now, if I execute the following statement:
SELECT TIMESTAMPDIFF( HOUR , edit_time, NOW( ) ) AS diff_in_hours
FROM data_received;
I get result: NULL
Can someone please help me understand what is happening?
That is because 0000-00-00 00:00:00 is not a valid DATETIME expression accepted by the function.
Please check documentation.
Use this method to keep edit-time in mysql
`edit_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Auto-update time'
Mysql tends to insert rubbish in an attempt to be "helpful". Best to tell it not to do that by settings mysql to STRICT mode so it behaves more like a database should.
SET sql_mode='STRICT_ALL_TABLES'
For details please read the manual.
0000-00-00 00:00:00 will probably interpreted as null. And comparing null in a TIMESTAMPDIFF will lead to null result.
MySQL doc says
In MySQL, the zero date is defined as '0000-00-00', even though this date is itself considered invalid.
There is no zeroth of zero in year zero, so you cannot calculate the time since then:
SELECT TIMESTAMPDIFF(HOUR, '0000-00-00 00:00:00', NOW())
NULL
Apparently MySQL does not have a problem with year 0 itself, as this returns a result:
SELECT TIMESTAMPDIFF(HOUR, '0000-01-01 00:00:00', NOW())
17643758