PRINT vagaries

  • I was surprised to find that '-' converts to 0. I expected the print statement would have producted "Conversion failed when converting the varchar value '-' to data type int." You could've tricked us by putting that as one of the choices.

  • Hehe, got me too. Never dreamed that '-' and '+' converts to 0. Tho it makes perfect sens when you think about it!

  • kimetch (6/30/2010)


    I was surprised to find that '-' converts to 0. I expected the print statement would have producted "Conversion failed when converting the varchar value '-' to data type int." You could've tricked us by putting that as one of the choices.

    Interestingly, you get an overflow when working with decimal instead:

    SELECT CAST('-' AS decimal(38,19))

    Best Regards,

    Chris Büttner

  • cengland0 (6/30/2010)


    info 25880 (6/30/2010)


    Yes, that's reason, because the result of an implicit casting of '-' is zero

    SELECT 6 + 1 + '-'

    So if you substitute the '-' with something clearly a string like 'a', you get a conversion failed message.

    My first thought was that the correct answer would have been the conversion failed because of the dash. When it was not in the options of possible answers, I guessed incorrectly with the 6-1. Always nice to learn something new.

  • I agree with the posts saying that '5' should've been an option as I would've definitely chose that. When I first read through the answer list, and saw that 5 wasn't an option, I thought maybe it was printing the values as strings so chose 6-1, but understand now after reading the description that the math ends up being 6+0+1.

    Great question - learned something.

  • What I'm not quite getting is why "PRINT 6 + 2 + + 7" produces 15, instead of some kind of arithmetic error. I don't think I could create a C# statement like that without the compiler throwing an error...

  • powell_todd (6/30/2010)


    What I'm not quite getting is why "PRINT 6 + 2 + + 7" produces 15, instead of some kind of arithmetic error. I don't think I could create a C# statement like that without the compiler throwing an error...

    Why would you expect an error?

    Here is your query with the formatting that might help you understand what is going on:

    PRINT 6 + 2 + (+7)

    The last plus is not used as "addition" operator, it is used as "positive" operator:

    http://msdn.microsoft.com/en-us/library/ms188400.aspx

    You can even build something like this:

    SELECT +++++++-+++++-1

    Best Regards,

    Chris Büttner

  • Good question...

    Also, why does this return 1...

    select 6+2+-+7

  • Thanks for the reply, and I understand that in the example of 6 + 2 + + 7, it's acting like it's placing the parens like you indicate. However, for the question of the day, it treated '-' as a "+0", rather than a minus sign.

    I think it would be more consistent if "PRINT 6 + '-' + 1" was considered as "PRINT 6 + -(+1)" which I think would produce "5". Seems like it does the operator thing in one case, and the "0" thing in another.

  • Christian Buettner-167247 (6/30/2010)


    powell_todd (6/30/2010)


    What I'm not quite getting is why "PRINT 6 + 2 + + 7" produces 15, instead of some kind of arithmetic error. I don't think I could create a C# statement like that without the compiler throwing an error...

    Why would you expect an error?

    Here is your query with the formatting that might help you understand what is going on:

    PRINT 6 + 2 + (+7)

    The last plus is not used as "addition" operator, it is used as "positive" operator:

    http://msdn.microsoft.com/en-us/library/ms188400.aspx

    You can even build something like this:

    SELECT +++++++-+++++-1

    Makes sense... thanks.

  • powell_todd (6/30/2010)


    Thanks for the reply, and I understand that in the example of 6 + 2 + + 7, it's acting like it's placing the parens like you indicate. However, for the question of the day, it treated '-' as a "+0", rather than a minus sign.

    I think it would be more consistent if "PRINT 6 + '-' + 1" was considered as "PRINT 6 + -(+1)" which I think would produce "5". Seems like it does the operator thing in one case, and the "0" thing in another.

    The reason for that is the mathematical statement is technically 6 + <string minus sign converted to an int, which would be 0> + 1 = 7

    If the single quotes were removed and the SQL engine was not converting string '-' to an int then the answer would've been 5 (6 + - + 1)

  • The reason for that is the mathematical statement is technically 6 + <string minus sign converted to an int, which would be 0> + 1 = 7

    If the single quotes were removed and the SQL engine was not converting string '-' to an int then the answer would've been 5 (6 + - + 1)

    Ah, that makes sense (in a weird sort of way). I still think I'd have a big problem if reviewing a SQL statement that looked like that. Thanks!

  • Yeah, this QotD threw me for a loop too and I got it wrong, but that's why I love the QotD's because right or wrong, I always learn something new from reading what everyone else is posting about the question... which I do recommend (reading what everyone's posting about the question) because sometimes the question and/or explanation is incorrect.

  • Great question. This is what QOD is all about.

    Converting oxygen into carbon dioxide, since 1955.
  • It's easy, just two unary operators before 7:

    select 6+2+(-(+7)) --> select 6+2+(-7) --> select 6+2-7

    lbrigham (6/30/2010)


    Good question...

    Also, why does this return 1...

    select 6+2+-+7

Viewing 15 posts - 16 through 30 (of 49 total)

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