Not In

  • Nice question and right answer. But, as a couple of people have pointed out, the explanation is wrong and misleading (it certainly misled omyrox, as this comment makes clear).

    Tom

  • I don't need to repeat how the actual explanation is incorrect, as this has been pointed out already. But I don't think anyone has covered what will happen in case of a type mismatch between the operand before the IN and (one of) the operands inside the IN.

    In such a case, the operand with lower data type precedence will be implicitly converted to the higher predence. If that's successful, the query returns the expected result. If not, it will fail with a runtime error.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Thanks for the question, made me think and research for a while.

    To me the explanation was clear. When stating

    s.Statuses NOT IN (null, 'Idle')

    you are trying to compare a data column attribute/property (null) against the actual value of the column, which returns UNKNOWN. Thus, UNKNOWN not being True (neither False), it will not set any row as selectable to return to the SELECT statement.

    Thanks,

    "El" Jerry.

    "El" Jerry.

    "A watt of Ottawa" - Gerardo Galvan

    To better understand your help request, please follow these best practices.[/url]

  • Dang it. Should have thought about this one a little more. Good question.

  • interesting question.

    Thanks

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • good question. dealt with that issue in some code on Monday! :w00t:

    Cheers

  • I really enjoyed this one. Thanks!

  • Stuart Davies (3/1/2012)


    Good question - it reminds me why I do not like nulls

    +1

    Thanks

  • tilew-948340 (3/1/2012)


    Stuart Davies (3/1/2012)


    Good question - it reminds me why I do not like nulls

    +1

    Thanks

    +1 ... Thanks 🙂

    Thanks

  • Great question, thanks!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • very good question!!!!

    thanks!!!!


    [font="Times New Roman"]rfr.ferrari[/font]
    DBA - SQL Server 2008
    MCITP | MCTS

    remember is live or suffer twice!
    the period you fastest growing is the most difficult period of your life!
  • I got this right b/c of a great article I've kept bookmarked, "Ten Common SQL Programming Mistakes":

    http://www.simple-talk.com/content/print.aspx?article=783

    Ratchev lists it as item #1, with a straightforward, lucid explanation as to why the query fails, giving a few different T-SQL approaches to correctly query NOT IN for nullable columns.

    Thanks,

    Rich

  • Thank you for the question. It made me realize that NOT IN (x, y) really means NOT IN x AND NOT IN y.

    I was thinking more like NOT IN x OR NOT IN y.

  • Tough question. Thanks for submitting.

    http://brittcluff.blogspot.com/

  • You have made a mistake in the correct answer!! The 'not in' was not referring to id. 5 points to me.

Viewing 15 posts - 31 through 45 (of 48 total)

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