Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a date is upcoming or past in MySQL

Tags:

sql

mysql

I have two columns in a table

  • event_date ( value is 09/22/2013)
  • event_time ( value is 7:11 PM )

Now I want to write a query which should returns row which has time and date in future or past.

I wrote something like this

SELECT *
FROM events e
WHERE CONCAT(e.event_date, ' ', e.event_time) <= DATE_FORMAT(NOW(), '%m/%d/%Y  %g:%i %a')

But it didn't work, it's just one of those things which you never know what's wrong with them

Thanks in advance!

like image 348
imran Avatar asked Jul 16 '13 00:07

imran


1 Answers

SELECT * 
FROM Events
WHERE event_date < CURRENT_DATE()
      OR (event_date = CURRENT_DATE() AND event_time <= CURRENT_TIME())
   

This has the advantage of using any index that might exist on event_date.

like image 196
Curt Avatar answered Sep 23 '22 02:09

Curt