Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL add 12 hours to a time field

Tags:

mysql

I need to add 12 hours to a MySQL TIME field (not DATETIME) and I'm having trouble.

UPDATE `events` 
SET start_time = DATE_ADD(start_time, INTERVAL 12 HOUR)
WHERE `start_time` < '11:00:00'

returns with no errors but doesn't change anything, I think because start_time is a TIME field.

UPDATE `events` 
SET start_time = start_time + '12:00:00'
WHERE `start_time` < '11:00:00'

adds 12 seconds.

like image 671
sdfor Avatar asked Jan 30 '11 02:01

sdfor


People also ask

How do I add 2 hours to current time in SQL?

To add 2 hours in the current time, we will use the DATE_ADD() function. mysql> select DATE_ADD(now(),interval 2 hour);

How do I add hours to a TIMESTAMP in SQL?

Use the ADDTIME() function if you want to select a new datetime by adding a given time to a datetime/timestamp/time value.

How do I get last 10 minutes in SQL?

We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 10 minutes in the past.

Is there a time data type for MySQL?

The date and time data types for representing temporal values are DATE , TIME , DATETIME , TIMESTAMP , and YEAR . Each temporal type has a range of valid values, as well as a “zero” value that may be used when you specify an invalid value that MySQL cannot represent.


2 Answers

Try using ADDTIME instead of DATE_ADD. You could do SET start_time = ADDTIME(start_time, '12:00:00')

like image 187
nybbler Avatar answered Oct 17 '22 23:10

nybbler


UPDATE `events` 
SET start_time = start_time + INTERVAL 12 HOUR
WHERE `start_time` < '11:00:00'

The MySQL functions that accept INTERVAL arguments are mostly unnecessary; you can just add and subtract intervals with + and -.

like image 12
Aaron Adams Avatar answered Oct 17 '22 21:10

Aaron Adams