Trouble excluding from a where statement

  • I'm having a hard time even searching for an answer for this since I don't know how to phrase it...

    I'm working with a select ... where. And I don't know how to exclude particular scenarios.

    I'm sure it must be simple, but my brain is just not coming up with it.

    Very simplified version:

    Select * from MyTable

    but not if varA = 1 and varB = 'Green'

    So if varA = 1, include the record. And if varB = 'Green,' include the record.

    But if varA = 1 and varB = 'Green,' do NOT include the record.

    Help?

  • So, you only want to exclude rows where varA = 1 and varB = 'Green'.

    WHERE NOT (varA = 1 AND varB = 'Green')

    Assuming that varA and varB are actually columns here. If they are actually variables, then you will either get all rows - or none, depending on the values in the variables.

    If your requirements are to include all rows where varA = 1 and varB = 'Green', but only when they are not on the same row:

    WHERE (varA = 1 AND varB <> 'Green')

    OR (varA <> 1 AND varB = 'Green')

    Or - this should work, but test it...

    WHERE (varA = 1 OR varB = 'Green')

    AND NOT (varA = 1 AND varB = 'Green')

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Yes.

    WHERE NOT (varA = 1 AND varB = 'Green') is exactly what I was looking for.

    I knew it was something really simple. It was driving me nuts that I couldn't think of it. Thank you!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply