Question

I'm trying to limit results in a table to records with dates that don't overlap my data. As you can see in the screenshot below I'm trying to add a clause to filter out records that are equal to the "end" column. See the final line in the query for that.

I can't figure out why results still show the record in the screenshot. Can someone help me out explaining that? It's probably a syntax thing?

SQL Where clause not filtering

Was it helpful?

Solution

You basically have:

a OR b AND c AND d AND e AND f

... and you probably want:

(a OR b) AND c AND d AND e AND f

Reference: Operator Precedence

OTHER TIPS

AND has a higher operator precedence than OR has.

That means that your AND clauses will be interpreted first so that at the end there's something like

where (...) or (... and ... and ...)

Since the first condition before the OR (('2014-01-14 18:30:00' between start and end)) is met, your row shows up. Put both sides of the OR clause in parantheses and it should work as you want to.

The problem is the first part of your WHERE clause is passing that record. I believe you need to more your brackets around a bit.

I'm not completely sure on what you are trying to achieve with the data that you have provided, but an opening bracket after the OR keyword and a closing bracket right at the end should do it.

Like this:

where
('2014-01-14 18:30:00' between start and end)
or (('2014-01-14 19:30:00' between start and end)
and ('2014-01-14 18:30:00' != start)
and ('2014-01-14 19:30:00' != end)
and ('2014-01-14 19:30:00' != start)
and ('2014-01-14 18:30:00' != end))

Because 2014-01-14 18:30 is between 2014-01-14 16:00 and 2014-01-14 18:30 which is a TRUE condition. And [(TRUE) Or (whatever else)] is also TRUE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top