Double plus ungood

  • I'm using SSMS pointed to SQL Server 2012. Can anyone tell me why both of these return the same value? In otherwords, why doesn't the second snippet cause an error?

    Select'XXX' + 'YYY'

    Select'XXX' + + 'YYY'

    Thanks

  • Interesting, sorry I don't have the answer.

    more...

    SELECT 1 + - 2

    SELECT 1 + - + 2


    Kindest Regards,

    Ian Smith

  • Unary plus and negative: https://msdn.microsoft.com/en-us/library/ms174362.aspx

    Cheers!

    EDIT: I must say that it is still strange that the unary plus works with string data, since the documentation specifies numeric data. However, it also basically says that it's a no-op, so maybe it's just ignored in that case. You do get an error if you try to use the unary minus with string data.

    On that note, that seems to be the one use for the unary plus, namely separating the unary minus so that they don't become comments. Of course, you might as well just use parentheses in that case, or just not apply two consecutive unary minus operators, since that doesn't exactly change much 🙂

  • + can be either a binary or unary operator. The first one is the binary operator and the second is the unary operator.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Jacob Wilkins (2/4/2016)


    Unary plus and negative: https://msdn.microsoft.com/en-us/library/ms174362.aspx

    Cheers!

    EDIT: I must say that it is still strange that the unary plus works with string data, since the documentation specifies numeric data. However, it also basically says that it's a no-op, so maybe it's just ignored in that case. You do get an error if you try to use the unary minus with string data.

    On that note, that seems to be the one use for the unary plus, namely separating the unary minus so that they don't become comments. Of course, you might as well just use parentheses in that case, or just not apply two consecutive unary minus operators, since that doesn't exactly change much 🙂

    I assume that's because + can either be the arithmetic operator or string concatenation and the code to drop the unary + is processed before the need to identify whether it is behaving as the arithmetic or string operator.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • FYI, the third one errors out:

    Select 'XXX' + 'YYY'

    Select 'XXX' + + 'YYY'

    Select 'XXX' + - 'YYY'

    So with the first two, the + sign is doing concatenation, not math. The minus sign in the third one is trying to do math.

    This works too:

    Select 'XXX' + + + + + + + 'YYY'

  • drew.allen (2/4/2016)


    Jacob Wilkins (2/4/2016)


    Unary plus and negative: https://msdn.microsoft.com/en-us/library/ms174362.aspx

    Cheers!

    EDIT: I must say that it is still strange that the unary plus works with string data, since the documentation specifies numeric data. However, it also basically says that it's a no-op, so maybe it's just ignored in that case. You do get an error if you try to use the unary minus with string data.

    On that note, that seems to be the one use for the unary plus, namely separating the unary minus so that they don't become comments. Of course, you might as well just use parentheses in that case, or just not apply two consecutive unary minus operators, since that doesn't exactly change much 🙂

    I assume that's because + can either be the arithmetic operator or string concatenation and the code to drop the unary + is processed before the need to identify whether it is behaving as the arithmetic or string operator.

    Drew

    I thought something like that at first, but doesn't explain why this works:

    SELECT +'What''s all this?'

    Cheers!

  • Jacob Wilkins (2/4/2016)


    drew.allen (2/4/2016)


    Jacob Wilkins (2/4/2016)


    Unary plus and negative: https://msdn.microsoft.com/en-us/library/ms174362.aspx

    Cheers!

    EDIT: I must say that it is still strange that the unary plus works with string data, since the documentation specifies numeric data. However, it also basically says that it's a no-op, so maybe it's just ignored in that case. You do get an error if you try to use the unary minus with string data.

    On that note, that seems to be the one use for the unary plus, namely separating the unary minus so that they don't become comments. Of course, you might as well just use parentheses in that case, or just not apply two consecutive unary minus operators, since that doesn't exactly change much 🙂

    I assume that's because + can either be the arithmetic operator or string concatenation and the code to drop the unary + is processed before the need to identify whether it is behaving as the arithmetic or string operator.

    Drew

    I thought something like that at first, but doesn't explain why this works:

    SELECT +'What''s all this?'

    Cheers!

    Sure it does. The code is first parsed and then interpreted. The unary + is presumably dropped during parsing and then there is no need to interpret the string as numeric.

    And the reason 'XXX' + - 'YYY' doesn't work is that - is only ever mathematical.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • drew.allen (2/5/2016)


    Jacob Wilkins (2/4/2016)


    drew.allen (2/4/2016)


    Jacob Wilkins (2/4/2016)


    Unary plus and negative: https://msdn.microsoft.com/en-us/library/ms174362.aspx

    Cheers!

    EDIT: I must say that it is still strange that the unary plus works with string data, since the documentation specifies numeric data. However, it also basically says that it's a no-op, so maybe it's just ignored in that case. You do get an error if you try to use the unary minus with string data.

    On that note, that seems to be the one use for the unary plus, namely separating the unary minus so that they don't become comments. Of course, you might as well just use parentheses in that case, or just not apply two consecutive unary minus operators, since that doesn't exactly change much 🙂

    I assume that's because + can either be the arithmetic operator or string concatenation and the code to drop the unary + is processed before the need to identify whether it is behaving as the arithmetic or string operator.

    Drew

    I thought something like that at first, but doesn't explain why this works:

    SELECT +'What''s all this?'

    Cheers!

    Sure it does. The code is first parsed and then interpreted. The unary + is presumably dropped during parsing and then there is no need to interpret the string as numeric.

    Ah, I misread your post. That's as plausible an explanation as any; it would be nice if that behavior were fully documented, but it's not all that important 🙂

    Cheers!

Viewing 9 posts - 1 through 8 (of 8 total)

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