mysql edit time only from date [duplicate] - mysql

How can I update only the time in an already existing DateTime field in MySQL? I want the date to stay the same.

Try this:
UPDATE yourtable
SET yourcolumn = concat(date(yourcolumn), ' 21:00:00')
WHERE Id = yourid;

Try this:
UPDATE t1 SET DateTimeField = CONCAT(DATE(DateTimeField),' 12:34:56');

UPDATE myTable
SET myDateTime = ADDTIME(DATE(myDateTime), #myTimeSpan)
WHERE id = #id;
Documented on MySQl date functions MySQL docs

I have solved in this way:
UPDATE table
SET myDateTime = CONCAT_WS(' ',DATE(myDateTime), CURTIME())
WHERE id = #id;
Obviously you should change CURTIME() with your desired time.

UPDATE myTable
SET myDateTime = ADDTIME(myDateTime, #myTimeSpan)
WHERE id = #id;
For exact syntax of function, see this.

Try this:
UPDATE sms
SET entry_period_end_date= entry_period_end_date+INTERVAL 6 Hour
WHERE TIME(entry_period_end_date) = '06:00:00';

UPDATE `table`
SET time = ADDTIME(time, INTERVAL 13 Hour);

Well, exactly what you are asking for is not possible. The date and time components can't be updated separately, so you have to calculate the new DateTime value from the existing one so that you can replace the whole value.

MySQL DEV page shows functions like subtime and difftime
A sample code to back the time all posts in 3 hours is above:
UPDATE tablepost SET datepost = SUBTIME( datepost , '0 3:0:0' );
Note that values 0 dont alter the respective field. Take care this code, use select first to test these function.
Reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime

Asuming you have a DATE field and TIME field and want to inject the time into the date, try this:
UPDATE mytable
SET mydatefield = ADDTIME( DATE_FORMAT(mydatefield,'%Y-%m-%d 00:00:00'), mydatefield)
WHERE myid = ...

I used ADDTIME in the following way
Earlier in my cloud server, the DateTime was set to UTC but after changing the DateTime to Asia/Kolkata ie UTC 5:30 I wanted the same to reflect in my database tables.
I wanted to update the created_at and updated_at column by 5 hours 30 minutes. I did the following
To update all the rows of the table
UPDATE
products
SET
created_at = ADDTIME(created_at, '5:30:0'),
updated_at = ADDTIME(updated_at, '5:30:0')
You can omit the WHERE condition if you want to update all the records, but since my new records were updated with proper values. So only my rows below id less than 2500 must be updated
UPDATE
products
SET
created_at = ADDTIME(created_at, '5:30:0'),
updated_at = ADDTIME(updated_at, '5:30:0')
WHERE
id < 2500;

This what helped me. I convert time to minutes firstly: 150 for 2:30 am.
UPDATE lesson SET starts_at = DATE_ADD(Date(starts_at), INTERVAL 150 MINUTE)
Minutes are enough accurate for me, though you can use other units: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add

Related

MySQL - update the row's date and save the time

I want to update a row which has timestamp datatype named date_start. Here's an example:
date_start = 2017-04-26 12:34:11
I want to update it, but to save the time, so it'll look like this:
date_start = 2017-05-28 12:34:11
Any ideas? So far I tried extract method and concat method.
Best Regards..
If you just want to update the date component of the timestamp while retaining the time component then you can build the updated timestamp using string concatenation with TIME():
UPDATE yourTable
SET date_start = CONCAT('2017-05-28 ', TIME(date_start))
WHERE <some condition>
Demo

Replace values in MySQL colums from row x to row y

I'd need to replace post dates from wordpress post table.
There are >800.000 post entries with the same date because of a migration.
How can I replace the date by "from row x to row"?
For example:
row 1 - 10.000 should have date 2013-01-02 09:20:10
row 10.001 - 20.000 should have date 2013-02-05 12:30:21
and so on...
Or maybe replacing by post id?
I know there is a sql query to do this, but I can not remember which one and how to use it correctly.
try adding a LIMIT to the sql to update rows:
UPDATE {table}
SET {datefield} = "{desired date}"
WHERE {datefield} = "{bad date}"
LIMIT 10000;
this will update 10000 rows at a time with a new date as desired, however it's not particularly picky about which ones get updated in which order, generally it will be in the database's internal order which is (roughly) chronological.
is there any other part of the data you can use to determine which records should be updated with which date?
This is not what you asked for, but might be better. You can create distinct timestamps, as if a post has been created every X seconds:
update posts
set created = timestamp('2013-01-02 12:00:00') + interval id * 140 second
where 1=1
http://sqlfiddle.com/#!9/a6c7e0/2
You can even make them look random:
update posts
set created =
timestamp('2013-01-02 12:00:00')
+ interval id * 140 second
+ interval floor(rand()*140) second
where 1=1
http://sqlfiddle.com/#!9/b394c/1

Adding days to date column and result in new column?

I need to add 40 days to a column , which contains date in d-m-y format,
and insert result into new column . eg
$querydate="UPDATE services SET paymentdue_date=payment_date+30 DAY WHERE my condition ";
and i tried
$querydate="SELECT up_id=".$up_id.",DATE_ADD(payment_date,INTERVAL 30 DAY) AS paymentdue_date FROM up_services";
Column structure for payment_date and paymentdue_date is varchar10 utf8_unicode_ci
any suggestions?
correct query will be
$querydate="SELECT up_id,DATE_ADD(payment_date,INTERVAL 30 DAY) AS paymentdue_date FROM up_services where up_id='$up_id'";
you can't add compare in select like up_id=".$up_id." use where instead
if you want update query you can follow #saharsh shah answer
Try this:
UPDATE services
SET paymentdue_date = DATE_ADD(STR_TO_DATE(payment_date, '%e-%c-%y'), INTERVAL 30 DAY)
WHERE my condition
I tried running this query and it worked for me , updated my column with new :)
$querydate="UPDATE up_services SET paymentdue_date=DATE_ADD(payment_date,INTERVAL 30 DAY) WHERE my condition ";

Substract MYSQL timestamp fixed amount of hour where ID is something

I want to make a line that removes a specific timestamp for a specific ID, I use MariaDB and havn'ut figured out how to.
X = some hour maybe 2 hours
TIMESTAMP = name of the table
Y = user id
What I want is something like:
Remove X amount of hours from TIMESTAMP where the id is Y
my timestamp format is not date it's like = 1414254628
below is on same table.
timestamp = 1414254628
timestamp2 = 1413646379
hope to get help, thanks!
sorry about the code blocks but but I had error on posting this thread so I had to make them look like codes
If I understand correctly and you store epoch time in your timestamp column you can just subtract the required number of seconds from it
SELECT timestamp - 3600 * 2 newtimestamp -- subtract two hours
FROM users
WHERE user_id = 1
Here is SQLFiddle demo
...how do i actually update the value?
By using UPDATE
UPDATE users
SET timestamp = timestamp - 3600 * 2
WHERE user_id = 1;
Here is SQLFiddle

mysql: update timestamp if timestamp is beyond 10 hours

Wondering if there is a way to do this without using two hits to the sql database.
If a person views content the timestamp is recorded.
If the person views the same content again 2 hours later the timestamp is not updated.
If the person views the same content 10 hours after first viewing the content, update the timestamp db table field.
Any method of doing this via SQL and not doing a "select" than php comparison than an "update" ??
update mytable
set lastvisited=now()
where person='john' and lastvisited<(now()-interval 10 hour);
Try
UPDATE tabel_name SET column_name=NOW() WHERE TIMESTAMPDIFF(HOUR, NOW(), column_name) >= 10