1

I have a table with with details of my companies products, and one of the columns has categories for the products but also empty strings for products that have not yet been categorized.

I'm trying to filter out the category 'trash' and leave the rest including the empty ones, but when I apply the filter, the empty ones are disappearing too... is this normal? how can I filter out the 'trash' and leave everything else including empty values?

I'm using a WHERE clause like this:

WHERE 
    table.month = 8 AND table.category <> 'trash'
6
  • 3
    Show us your data. I Guess the disappeared items have NULL values. Commented Aug 12, 2016 at 9:12
  • Are you sure that there are items for month = 8 that have category <> 'trash'? Commented Aug 12, 2016 at 9:16
  • 2
    @ThorstenDittmar Nope, not in SQL Server as you can see here. data.stackexchange.com/stackoverflow/query/523606 Commented Aug 12, 2016 at 9:23
  • You're both right, of course... Commented Aug 12, 2016 at 9:25
  • 1
    @TedoG. Sorry, I got mixed up here. Had to bring the extended logic tables back to my mind... :-) Commented Aug 12, 2016 at 9:28

1 Answer 1

4

Probably you have entries with category NULL. Thus, they will not be returned, because by definition the comparison null <> 'trash' is neither true nor false, but a WHERE clause will only return records where all conditions are true.

You could modify your statement to this:

WHERE table.month = 8 AND ISNULL(table.category, '') <> 'trash'

or this:

WHERE table.month = 8 AND (table.category is null OR table.category <> 'trash')

This former replaces null values with empty strings, which should return what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much, the first statement worked perfectly. I had no idea that NULLs would behave like this. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.