Return row in given date

Posted on

Question :

I have a MySQL table named tasks that contains four columns:

id (Integer)
name (Varchar)
task_start (DateTime)
task_finish (DateTime)

How can I write an SQL statement that returns tasks that are running at a specific point in time (e.g. task that are running right now)?

Answer :

You can do it this way

-- Tasks that were running at specific (e.g. 2014-02-01 12:15:00) point in time 
SELECT id, name, task_start, task_finish
  FROM tasks
 WHERE '2014-02-01 12:15:00' BETWEEN task_start AND task_finish;

-- Tasks that are running right now
SELECT id, name, task_start, task_finish
  FROM tasks
 WHERE NOW() BETWEEN task_start AND task_finish;

Here is SQLFiddle demo

task that are now running

select * from tasks where task_start is not null and task_finish is null

Leave a Reply

Your email address will not be published.