Brandon
Brandon

Reputation: 117

Where statement in my query is supposed to return only today's and yesterday's dates, but is still returning earlier dates

I am looking to only retrieve data from the past 24 hours. The WHERE statement I am using, in theory, should retrieve only from those productiondates. However, I am still having week-old productiondates returned. Any thoughts on how to improve this, or am I doing it wrong? I am using periscope.

select example1, 
    example2, 
    example3,
    productiondate,
    example4, 
    example5
from final
where exampleX = exampleY or exampleX is null
and productiondate  > DATEADD(day,-1, GETDATE())
and example1 <> 'XXX'
and example2 <> 'YYY'
and example2 <> 'ZZZ'
  
order by 2

Upvotes: 0

Views: 38

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415665

Logical operator precedence in SQL can be surprising. You need parentheses around the OR.

where (exampleX = exampleY or exampleX is null)

Alternatively, you could do this:

where coalesce(exampleX, exampleY) = exampleY

Upvotes: 1

Related Questions