Find and replace data in a MySql database - mysql

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

Related

MySQL - How to select rows where datetime field is not equal to 0000-00-00 00:00:00?

Here is my table "tb_posts":
I want to select only those rows where datetime field i.e. post_date_published is not equal to 0000-00-00 00:00:00. I am using following query but it doesn't work:
SELECT * FROM `tb_posts` WHERE `post_date_published` IS NOT NULL
I am getting the same output as shown in the above picture.
Why IS NOT NULL is not working?
As per the MYSQL documentation it saves invalid dates as '0000-00-00 00:00:00'. It will not be considered as NULL.
Try comparing with the date '0000-00-00 00:00:00':
SELECT * FROM tb_posts where post_date_published != '0000-00-00 00:00:00'
A method I use with this sort of thing is
SELECT `columns` FROM `tb_posts` WHERE UNIX_TIMESTAMP(`post_date_published`) > 0
From the MySQL Documentation:
The valid range of argument values is the same as for the TIMESTAMP
data type: '1970-01-01 00:00:01.000000' UTC to '2038-01-19
03:14:07.999999' UTC. If you pass an out-of-range date to
UNIX_TIMESTAMP(), it returns 0.
The UNIX_TIMESTAMP function forces the result to be an integer so it's much easier to work with in these quick comparisons. It is also vital for working with MySQL 5.7 where "empty" (ie zero value) date/time columns are not allowed.
(I had a lot of grief trying to convert various date columns to NULL because MySQL 5.7+ didn't recognise 0000-00-00 00:00:00 as a valid comparison -- so I converted it to a unix timestamp so as to compare the timestamp rather than the actual [invalid] date.)

conversion of epoch_time gives '1970-01-01' in mysql

I've column name epoch_time with a varchar(255) which is having approx 40,000 epoch time in it and I've tried to convert all of them into a equivalent date with this query UPDATE table_name SET converted_into_date = FROM_UNIXTIME(epoch_time);
but it is giving 1970-01-01 05:30:00 in all rows and the datatype of converted_into_date column is varchar. I've tried all the datatype but then it is returning null . working in maria_db.
Please try
SELECT FROM_UNIXTIME(1526019841742682/1000000)

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;

Is A Datetime Column's Value the Default One

I have a MySQL table and a column of type datetime. It's default value is something like 0000-00-00 00:00:00.
How do I check if a given row's value on this datetime column is the above by using native MySQL query functionality. E.g. without using "SELECT * FROM table WHERE my_date<>'0000-00-00 00:00:00'", because this leaves room for errors on different MySQL servers and configurations I believe.
SELECT * FROM table WHERE my_date <> 0
You can test it with
select cast('0000-00-00 00:00:00' as datetime) = 0
which returns true (and false for all other datetime values).
You could do something like "SELECT * FROM table WHERE my_date > '1970-01-01 00:00:00". 1/1/1970 is the commonly used epoch date.