concatenation

  • Nice question :-). Thanks.


    Sujeet Singh

  • Nice question, learned something new today. I was quite sure the answer would be 251, but then I remembered data type precedence may be tricky.

    Toreador (8/21/2012)


    sestell1 (8/21/2012)


    Implicit conversion isn't always your friend.

    Implicit conversion isn't ever your friend!

    Implicit conversion isn't even your friend! πŸ™‚

    "El" Jerry.

    "A watt of Ottawa" - Gerardo Galvan

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

  • good and easy question, thanks

    --------------------------------------
    ;-)β€œEverything has beauty, but not everyone sees it.” ― Confucius

  • Nice question about the basics. Thanks!

  • EL Jerry (8/21/2012)


    Nice question, learned something new today. I was quite sure the answer would be 251, but then I remembered data type precedence may be tricky.

    Toreador (8/21/2012)


    sestell1 (8/21/2012)


    Implicit conversion isn't always your friend.

    Implicit conversion isn't ever your friend!

    Implicit conversion isn't even your friend! πŸ™‚

    I unfriended Implicit Conversion long ago.

  • I got it wrong on the assumption that data type conversion would take precedence over concatenation or addition. Now I understand that while I'm correct about that, the precedence does not apply to the entire statement at once, but rather on a series of left-to-right pairs. Makes sense, I suppose.

    Fortunately for me I would never consider doing this implicitly. That's just asking for trouble.

    ron

    -----
    a haiku...

    NULL is not zero
    NULL is not an empty string
    NULL is the unknown

  • I am not a T-SQL person since I am primarily an Oracle DBA, but I did notice the single quotes and it make me think. The equivilent SQL in Oracle produces the same results: select '130' || '120' + 1 from dual;

    HTH -- Mark D Powell --

  • Great tricky question!

    :exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:

  • Try this one: SELECT 1+'130'+'120'+1. It comes up 252! Implicit conversion are very dangerous.

  • (Bob Brown) (8/21/2012)


    Try this one: SELECT 1+'130'+'120'+1. It comes up 252! Implicit conversion are very dangerous.

    I would expect it to.

    1 + '130' = 131 ('130' converted to int)

    + '120' = 251 ('120' converted to int because the result above is an int)

    + 1 = 252 πŸ™‚



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • ronmoses (8/21/2012)


    I got it wrong on the assumption that data type conversion would take precedence over concatenation or addition. Now I understand that while I'm correct about that, the precedence does not apply to the entire statement at once, but rather on a series of left-to-right pairs. Makes sense, I suppose.

    Fortunately for me I would never consider doing this implicitly. That's just asking for trouble.

    ron

    Bit me for the same reason. Agree, I wouldn't do this implicitly either.

  • Really interesting one - thanks!

  • It's very interesting how this starts as a concatenation theme, but now it has become an implicit conversion issue. By the way, the only implicit conversion in wich I trust is string to datetime. It will always works if the string provided is in 'yyyymmdd' format.

    declare @s-2 nvarchar(8)

    , @d1 datetime

    , @d2 datetime

    , @d3 datetime

    set @s-2 = '20120821'

    set dateformat dmy;

    set @d1 = @s-2

    select @d1 as date1

    set dateformat mdy;

    set @d2 = @s-2

    select @d2 as date2

    set dateformat ymd;

    set @d3 = @s-2

    select @d3 as date3

    πŸ™‚

  • Toreador (8/21/2012)


    sestell1 (8/21/2012)


    Implicit conversion isn't always your friend.

    Implicit conversion isn't ever your friend!

    +1

    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

  • ronmoses (8/21/2012)


    I got it wrong on the assumption that data type conversion would take precedence over concatenation or addition.

    Made the same assumption. Since I had never implicitly converted like this, I was under the misapprehension that the datatype evaluation would go before the operations. Thanks to OP for posting, as I learned something today.

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

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

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