0

I am trying to pull data on certain requirements from some keys/IDS. ID's are either populated or they are null or -1. The data looks something like this

ID1    ID2     ID3 
3143   NULL    93484
-1     -1      NULL
1134   8493    4345    
NULL   NULL    NULL
-1     -1      -1

I have rules in place where I need to pull IDs if not null or equal to negative 1. Here is part of my query, which is not working.

    where (ID1 is not null or ID1 <> '-1' ) and  (ID2 is not null or ID2 <> '-1' )
     and  (ID3 is not null or ID3 <> '-1' )

The counts don't add up and it is not catching all of the IDS. Any thoughts on why?

Oh, the columns are all VARCHAR(250).

3
  • is the NULL a string value of 'NULL' by chance? if not the full query you are using may be helpful
    – xeo
    Aug 14, 2014 at 18:00
  • By using the AND clause, you're making it evaluate all the keys - thus, only the third row in your example would be returned. Is that your intent, or are you just trying to print out any IDs that aren't NULL or -1, regardless of what the others on the row are?
    – AHiggins
    Aug 14, 2014 at 18:01
  • Please post the full query and a better explanation of what you are trying to do here - several people have pointed out a possible flaw in your understanding of Boolean (AND vs. OR) logic, but unless we know what you're trying to do we won't be able to get more specific.
    – AHiggins
    Aug 14, 2014 at 18:10

1 Answer 1

2

Using and will force the query to return data that matches all the three "set" of conditions

where (ID1 is not null or ID1 <> '-1' ) or (ID2 is not null or ID2 <> '-1' )
or  (ID3 is not null or ID3 <> '-1' )

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.