T-SQL Fun

  • I believe the author was trying to point out confusion can result from negative logic tests. I have added a note to the explanation.

  • I thought there was a trick or something because this was not difficult logic, but using AND or OR can be made more complicated with inequalities. I think the author was just trying to point it out.

    Does anybody else think it's funny that people complain about trick questions and other complain about questions that aren't tricky enough? The reason for these questions is not to get a high point total on the website but to learn or be reminded of things to make us better SQL-ers.

  • I agree with you Dave, I am not understanding why it goes to "True" condition.

    False or False results False.

    75% of people saying it "True" is puzzling me.

  • It is true that experienced programmers can read and understand double negatives without much trouble. My first reaction was, 'of course it returns 'OK''.

    As we write code, we also are wise to think about those who will need to understand it or maintain it in the future.

    A single positive condition is more simple than a double negative.

    I once had a job where a primary duty was maintaining code that had been provided by an outside vendor. Overall, it was very well done and had some really great solutions in the code.

    But, the original programmer chose to have a field in nearly every table with a variant of this name 'INVALID_N_HLD_KEY'

    So, the code always went something like this:

    IF INVALID_N_HLD_KEY = 'Y' THEN . . .

    or

    IF INVALID_N_HLD_KEY = 'N' THEN

    So, if the code is Y, the validity is no. And, if the code is N, the validity is yes.

    That was a continuous speed bump for me in working with the system. I wish so much that the programmer had chosen the name 'VALID_N_HLD_KEY', and let yes be yes, and no be no.

  • I think this is a solid question that does not try to trick anyone but shows a simple example of a inequality comparison that would have been easier to read as an equality expression. Like many have stated it's not a complicated comparison but valid non-the-less.

    Venu Bode (9/16/2009)


    I agree with you Dave, I am not understanding why it goes to "True" condition.

    False or False results False.

    75% of people saying it "True" is puzzling me.

    The answer is false which goes to the else clause thus the "OK".

    - David

  • Just it needs bit attention... more than that i did not find any thing special here ...

  • I wasn't so much confused by the negative conditionss as by the variable names themselves. Using "@c" and "@var" put my mind in the frame of "We're going to compare a variable with a constant". So, the overall point of write readable code was being made in a second manner, even if unintentionally. Yes, I got it right, but I did have to put asided that initial assumption.

  • CirquedeSQLeil (9/15/2009)


    This one made me pause for a sec - i was sure there was a trick somewhere in it (an extra space, some syntax or something). But straightforward it is.

    Heh, me too. I spent ten minutes trying to figure out what the trick was. 🙂

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • CirquedeSQLeil (9/15/2009)


    This one made me pause for a sec - i was sure there was a trick somewhere in it (an extra space, some syntax or something). But straightforward it is.

    Heh, me too. I spent ten minutes trying to figure out what the trick was. 🙂

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • If it's tricks you're after, what would the results be for the following query?

    DECLARE @MyVars TABLE (c char(8), [var] char(8));

    INSERT INTO @MyVars (c, [var])

    SELECT 'I', 'E' UNION ALL

    SELECT 'Q', 'X';

    SELECT c, [Var]

    FROM @MyVars

    WHERE (c + [var] != 'IE');

    TroyK

Viewing 10 posts - 16 through 24 (of 24 total)

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