Ei Maung
Ei Maung

Reputation: 7153

Add 2 hours to current time in MySQL?

Which is the valid syntax of this query in MySQL?

SELECT * FROM courses WHERE (now() + 2 hours) > start_time

note: start_time is a field of courses table

Upvotes: 212

Views: 314940

Answers (5)

Soumyajit Swain
Soumyajit Swain

Reputation: 1340

This will also work

SELECT NAME 
FROM GEO_LOCATION
WHERE MODIFY_ON BETWEEN SYSDATE() - INTERVAL 2 HOUR AND SYSDATE()

Upvotes: 1

lc.
lc.

Reputation: 116498

The DATE_ADD() function will do the trick. (You can also use the ADDTIME() function if you're running at least v4.1.1.)

For your query, this would be:

SELECT * 
FROM courses 
WHERE DATE_ADD(now(), INTERVAL 2 HOUR) > start_time

Or,

SELECT * 
FROM courses 
WHERE ADDTIME(now(), '02:00:00') > start_time

Upvotes: 29

Glavić
Glavić

Reputation: 43552

SELECT * 
FROM courses 
WHERE DATE_ADD(NOW(), INTERVAL 2 HOUR) > start_time

See Date and Time Functions for other date/time manipulation.

Upvotes: 399

Daniël van Eeden
Daniël van Eeden

Reputation: 653

SELECT * FROM courses WHERE (NOW() + INTERVAL 2 HOUR) > start_time

Upvotes: 34

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

You need DATE_SUB() OR DATE_ADD()

Upvotes: 53

Related Questions