Josip Codes
Josip Codes

Reputation: 9521

Select date that's in given date's interval

I have a table of dates, and want to select dates that are in 1,5 hour interval of given date. For example:

-- '2013-06-11 18:40' is the given date, and I want all dates that are 
-- inside +/- 1.5 hour interval
SELECT datum 
FROM table 
WHERE date(datum) > date( date_sub( '2013-06-11 18:40', interval 1.5 hour ) )
AND date(datum) < date( date_add( '2013-06-11 18:40', interval 1.5 hour ) )

Upvotes: 0

Views: 337

Answers (2)

Andomar
Andomar

Reputation: 238196

The MySQL date function takes the date part of a datetime. That's not advisable when you're looking for a 3 hour interval :)

Try:

WHERE datum between date_sub( '2013-06-11 18:40', interval 90 minute) and
    date_add( '2013-06-11 18:40', interval 90 minute)

Upvotes: 2

user4035
user4035

Reputation: 23749

I used integer minutes instead of hours plus removed DATE function as Andomar suggested:

SELECT datum 
FROM table 
WHERE 
datum > date( date_sub( '2013-06-11 18:40', interval 90 MINUTE ) )
AND 
datum < date( date_add( '2013-06-11 18:40', interval 90 MINUTE ) )

Upvotes: 2

Related Questions