Fun addition

  • Comments posted to this topic are about the item Fun addition

  • Nice, easy question, thanks Steve

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    โ€œlibera tute vulgaris exโ€

  • Datatype precedence!! :ermm:
    Where is my coffee, please! ๐Ÿ˜€

  • I agree about the issue being datatype precedence.  Here's a useful list:
    https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-type-precedence-transact-sql?view=sql-server-2017

  • I was about to complain that the question should have specified which version of SQL Server. Otherwise the correct answer is either '2" or "an error".

    But I looked it up. I hadn't realized that += had been added as early as SQL Server 2008. So every supported release of SQL Server will allow this.

  • Thanks, nice question.

    Btw this returns 11 (or rather, '11'):
    DECLARE @s-2 VARCHAR(10) = '1'
    SELECT @s-2 += '1'
    SELECT @s-2

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Hah hah, very witty - thank you, Steve!

  • simples ๐Ÿ™‚

    ---------------------------------------------------------------------------------------
    The more you know, the more you know that you dont know

  • webrunner - Tuesday, December 4, 2018 8:30 AM

    Thanks, nice question.

    Btw this returns 11 (or rather, '11'):
    DECLARE @s-2 VARCHAR(10) = '1'
    SELECT @s-2 += '1'
    SELECT @s-2

    - webrunner

    I must admit that I'm struggling with this question. The Concatenation answer of '11' makes more sense to me than the conversion into Int. I ran this this on a 2016 SSMS Version and got '11'.

  • webrunner - Tuesday, December 4, 2018 8:30 AM

    Thanks, nice question.

    Btw this returns 11 (or rather, '11'):
    DECLARE @s-2 VARCHAR(10) = '1'
    SELECT @s-2 += '1'
    SELECT @s-2

    - webrunner

    I suppose MS has implicit cast order documented somewhere. But this inspired me to do some testing. Where @s-2 is varchar and @i is int.
    @s-2 += '1' = '11'
    @s-2 += 1 = 2
    @i += '1' = '11'
    @i += 1 = 2

    I guess that's a bit intuitive. The type of the rhs value is being used to decide whether + is a string operator or a numeric operator.

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

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