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;
Related
MYSQL server version: 5.7.25.
I have table where default value for dt field is CURRENT_TIMESTAMP. Today found, it adding dates in UTC, and as found from forums, in order to have date in PDT, need add trigger. is following correct?
BEFORE INSERT ON portin_failed_email_sent
FOR EACH ROW SET new.dtg_pdt = IFNULL(new.dtg_pdt,DATE_SUB(new.dt, INTERVAL 7 HOUR ));
I want to update a date field and set it to 2018-03-22 00:00:00 but I get the following stupid error:
Error Code: 1292. Incorrect datetime value: '2018-03-22 00:00:00' for column 'Date' at row 158917
This is the query I use for updating:
update assets.transactions
set date = date_add(date, interval 1 hour)
where date between '2018-03-21 23:00:00' and '2018-06-29 23:59:59';
What is wrong? I searched a lot and found out dates before 1970-01-01 00:00:01 are not supported by MySQL, that is acceptable, but a date in the middle of 2018? That's something I can't digest.
Is there any solution to make this work right?
I guess you're updating a TIMESTAMP column. I also guess you have your MySQL instance set to a timezone with a daylight time switchover on 23-March-2018. I guess the rules of your timezone switchover in your country mean that the clock rolls over from 21-March-2018 11:59:59 to 22-March-2018 01:00:00.
So the value 2018-03-22 00:00:00 just doesn't exist.
Strange, isn't it?
Try issuing this MySQL command, to set the time zone to UTC, before doing these sorts of mass timestamp updates.
SET time_zone = 'UTC';
Don't forget to switch it back before doing other operations. Or just do those operations from a different MySQL connection.
select count(*) from PN_Review where addTime >= '2017-05-01 00:00:00';
result:3468
select count(*) from PN_Review where addTime >= 1493568000;
result:37645 which is exactly larger than fact.
addTime column is created by timestamp
what I do wrong with second query?
Here's what's happening with that second query.
MySQL to accessing every row in the table, and converting the value in the addTime (TIMESTAMP) column into a string value. And then comparing the string value to a literal `'1493568000'.
addTime >= 1493568000
that's essentially the same as specifying `
addTime >= '1493568000'
which is equivalent to
DATE_FORMAT(addTime,'%Y-%m-%d %T) >= '1493568000'
All of those will evaluate to TRUE for an addTime value of '1970-01-02', and for any non-NULL value other than '0000-00-00'. And MySQL has to perform that conversion and comparison for every row in the table.
Consider adding MIN(addTime) to the SELECT list, along with the COUNT(*), and I expect the value returned will differ from the value returned by the first query.
With the expression in the first query:
addTime >= '2017-05-01 00:00:00'
MySQL evaluates the literal string on the right side as a datetime value in the current timezone, and then compares that to the value of addTime TIMESTAMP column. (With this form, MySQL can make use of an index (with addTime as the leading column) to perform a range scan operation.)
Here are some other forms to consider:
UNIX_TIMESTAMP(addTime) >= 1493568000
The UNIX_TIMESTAMP function will return the internal integer "seconds" value from the TIMESTAMP,and compare that to the numeric literal.
addTime >= FROM_UNIXTIME(1493568000)
The value returned by the FROM_UNIXTIME function is influenced by the setting of the session time_zone variable. The returned value is compared to the TIMESTAMP column.
Consider this demonstration:
SET time_zone = '+05:00' ;
SELECT ##time_zone, FROM_UNIXTIME(1493568000) ;
returns
##time_zone FROM_UNIXTIME(1493568000)
----------- -------------------------
+05:00 2017-04-30 21:00:00
compare to
SET time_zone = '+00:00' ;
SELECT ##time_zone, FROM_UNIXTIME(1493568000) ;
which returns
##time_zone FROM_UNIXTIME(1493568000)
----------- -------------------------
+00:00 2017-04-30 16:00:00
I try to get following query to work, but 0 rows are affected:
UPDATE table_x
SET sql_date = DATE(STR_TO_DATE(date_string,'%Y-%m-%d'))
WHERE sql_date = '0000-00-00'
The date format in the column date_string is: '%d.%m.%Y'
What am I doing wrong?
Thanks
your column date_string is already "yyyy-dd-mm", then why you want to update.
Still you can compare date as per below:
UPDATE table_x set sql_date = DATE(STR_TO_DATE(date_string,'%Y-%m-%d'))
WHERE date(sql_date) = '0000-00-00';
SO it will skip time stamp and compare only date with your passes date.
Date function will get only date from the stored date. So if "1992-11-06 00:00:00" is stored in DB then it will compare only "1992-11-06".
You say date_string is in the format '%d.%m.%Y'. But you are mistakenly specifying '%Y-%m-%d' for STR_TO_DATE instead. Moreover when you have converted the string to date you apply DATE on this date. Why?
UPDATE table_x
SET sql_date = STR_TO_DATE(date_string, '%d.%m.%Y')
WHERE sql_date = '0000-00-00';
I have a datetime 0000-00-00 00:00:00 column in my table,
Need to keep the minute and seconds when updating ____-__-__ __:MM:SS
but also change the date and hour to current time.
How can I achieve this on MySQL side?
Edit (Sample):
Current Date field value: 2016-06-27 15:13:07
We Update this table at 2016-07-28 12:31:18
Desired Date field value: 2016-07-28 12:13:07
As you can see the updated date still has correct 2016-07-28 12: but the :13:07 (minutes and seconds) are Selected from the date before update and replaced by current time's minutes and seconds
You can make use of MySQL CONCAT() function. Your update query would looks something like this,
UPDATE table_name
SET
date_column = CONCAT('2016-06-29 15:',MINUTE(date_column),':',SECOND(date_column))
WHERE
column_id = 1
CONCAT() returns the string that results from concatenating the given arguments.
So in your case we would update date and hour by adding first argument as 2016-06-29 15:, and then use the same minute and second from the same column. And concatenate all the arguments to make the new value as you need.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat