comparing strings using <> operator, not getting expected results

  • Wonder if anyone can help me with this problem, Not getting the expected results.

    CREATE TABLE #temp

    (

    [state] varchar(25),

    [county] varchar(25)

    )

    Insert into #temp

    ([state], [county])

    values

    ('nevada','clark'),

    ('missouri','clark'),

    ('california','clark'),

    ('nevada','las Vegas')

    select * from #temp

    where ( [state] <> 'nevada' AND [county] <> 'clark')

    I would expect to get 3 records back, instead no results return.

    state county

    ------ --------

    missouri clark

    california clark

    nevada las vegas

    What am I doing wrong?

  • Why would you expect 3 results? All of your rows are either in the state of Nevada, or the county of clark. Do you mean to exclude rows where either of these conditions is true, rather than both of them? If so, you need to use OR:

    select * from #temp

    where ( [state] <> 'nevada' OR [county] <> 'clark')

    Or, you could look at it another way and say I want to return everything except where the state='nevada' AND the county = clark, in which case you could use:

    select * from #temp

    where NOT ( [state] = 'nevada' AND [county] = 'clark')

  • 2nd Statement works we will use that one Thanks for your input. 😎

  • That worked and gave me the expected results. Thank you very much!

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

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