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;
Related
I'm running xampp 3.3.0, mysql 8.0. In my database, there is a column datetime with type "datetime". I'm trying to insert data in it with CURRENT_TIMESTAP.
Example of query:
INSERT INTO `statistics` (`chat_id`, `user_id`, `message_id`, `datetime`, `ai_unique`) VALUES ('988', '767', '98765', 'CURRENT_TIMESTAMP', NULL);
It inserts, but I'm getting
Warning: #1265 Data truncated for column 'datetime' at row 1
And instead of current timestamp, it inserts 0000-00-00 00:00:00
Some time ago it worked just fine, but now it's inserting 0000-00-00 00:00:00. If I manually write a date here like 2023-01-11 22:52:01 it works just fine
'CURRENT_TIMESTAMP' (with single-quotes) is a string. It doesn't have a datetime value. MySQL makes an effort to convert the string to a datetime, just as it would convert the string '2023-01-24 13:09:00' to a datetime. But the string you used cannot be converted in this way.
CURRENT_TIMESTAMP (a pseudoclumn with no single-quotes) has a datetime value.
CURRENT_TIMESTAMP() (a function) also returns a datetime value.
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
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;
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;
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