John
John

Reputation: 10146

Mysql - Selecting rows that dont match a date range?

I have a table in mysql that holds a start and end date field. What I need to do is query this table to find all rows that do not match the date Im searching by. How would I do that? So if I used todays date and queried this table with it then I need to find all rows that this date does not fall between the start and end date in the table. Hope that makes sense!

Upvotes: 1

Views: 1534

Answers (4)

metalfight - user868766
metalfight - user868766

Reputation: 2750

One more variant:

select id, start_date, end_date 
from my_table 
where end_date < CAST('2012-01-01' AS DATE) 
or start_date > CAST('2012-12-31' AS DATE)

Upvotes: 0

Omesh
Omesh

Reputation: 29071

You can use BETWEEN clause to filter records based on date range and to exclude that date range you can use NOT clause as:

SELECT * 
FROM my_table 
WHERE search_date NOT BETWEEN start_date AND end_date;

Upvotes: 3

Art Licis
Art Licis

Reputation: 3679

To select range you normally use BETWEEN, and if you want to exclude those values, you can simply use NOT BETWEEN.

Upvotes: 0

podiluska
podiluska

Reputation: 51494

Select *
from table
where [todaysdate]<datestart
or [todaysdate]>dateend

Upvotes: 0

Related Questions