concatenation

  • Divine Flame

    SSCoach

    Points: 15941

    Nice question :-). Thanks.


    Sujeet Singh

  • EL Jerry

    SSCertifiable

    Points: 7057

    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]

  • chgn01

    Hall of Fame

    Points: 3725

    good and easy question, thanks

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

  • Ken Wymore

    SSCoach

    Points: 16666

    Nice question about the basics. Thanks!

  • Cliff Jones

    SSChampion

    Points: 10517

    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.

  • ronmoses@gmail.com

    SSCarpal Tunnel

    Points: 4480

    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

  • Mark D Powell

    SSCarpal Tunnel

    Points: 4454

    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 --

  • Robin Sasson

    Hall of Fame

    Points: 3359

    Great tricky question!

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

  • (Bob Brown)

    SSCrazy

    Points: 2705

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

  • mtassin

    SSC-Insane

    Points: 23099

    (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]

  • Lynn Pettis

    SSC Guru

    Points: 442361

    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.

  • Revenant

    SSC-Forever

    Points: 42467

    Really interesting one - thanks!

  • Narud

    SSCrazy

    Points: 2826

    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 nvarchar(8)

    , @d1 datetime

    , @d2 datetime

    , @d3 datetime

    set @S = '20120821'

    set dateformat dmy;

    set @d1 = @S

    select @d1 as date1

    set dateformat mdy;

    set @d2 = @S

    select @d2 as date2

    set dateformat ymd;

    set @d3 = @S

    select @d3 as date3

    πŸ™‚

  • SQLRNNR

    SSC Guru

    Points: 281252

    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

  • Thomas Abraham

    SSChampion

    Points: 10761

    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 35 total)

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