What will the results be for the code below?

  • UMG Developer

    SSChampion

    Points: 13482

    Comments posted to this topic are about the item What will the results be for the code below?

  • Ron McCullough

    SSC Guru

    Points: 63877

    Good question ...

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Dhruvesh Shah

    SSCrazy

    Points: 2243

    Good Question. Explanation clears all the doubts

  • sharath.chalamgari

    SSCertifiable

    Points: 5680

    Good Question with Proper Explanation. Thank u

  • Toreador

    SSChampion

    Points: 11231

    Good question and I learnt something ๐Ÿ™‚

    I wonder how many of those who got it right (70% at the moment) did so without running the script first!

  • Hardy21

    SSCrazy Eights

    Points: 9708

    Good Question & Nice Explanation. Thanks ๐Ÿ™‚

    Thanks

  • Koen Verbeeck

    SSC Guru

    Points: 258941

    Nice question!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • OCTom

    SSChampion

    Points: 11755

    Precedence, operation order, and implicit conversion. How many times have I been caught by those? Great explanation.

  • sjimmo

    SSChampion

    Points: 11139

    Great question and explanation. Reinforces the need to pay attention to detail.

    Steve Jimmo
    Sr DBA
    โ€œIf we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan

  • UMG Developer

    SSChampion

    Points: 13482

    Thanks for the comments. This was my first QotD and I was worried that my explanation might not have been clear enough. (The formatting is certainly an issue, but I don't think we can't format much, if any, in the explanation. Though I swear I had line breaks in there.)

  • john.arnott

    SSChampion

    Points: 11882

    The good thing is that I learned somethng here, and not about conversion precedence.

    I answered that the intial assignment would fail as the CONVERT function seemed wrong. The third parameter, 101 in this case, is optional for converting strings to datetime but could be necessary to resolve ambiguities when some dates are presented in formats other than the db's local standard. I'd wrongly thought that the third parameter only worked for converting datetime TO a string.

  • timothy bates

    SSC-Addicted

    Points: 464

    Nice QOTD.

    I got it right but for different reasons. If you wanted to add an additional wrinkle try:

    SELECT @Var2 = ' - ', @Var3 = CONVERT(DATETIME, '01/01/2010', 101),@Var1 = cast(@var3+5 as int);

    SELECT @Var1 + @Var2 + @Var3 AS Result1;

    If the perception is that this is performing date addition (e.g. Dateadd) the above expected result would be 5, not 2120-01-07 00:00:00.00.

    Tim

  • jts2013

    Hall of Fame

    Points: 3226

    A very good explantation - thanks. I got it right with a bit of guesswork, but hadn't realised that the operator precedence was the key.

  • homebrew01

    SSC Guru

    Points: 55137

    Toreador (11/1/2010)


    Good question and I learnt something ๐Ÿ™‚

    I wonder how many of those who got it right (70% at the moment) did so without running the script first!

    I did ๐Ÿ˜€ .... I've got my share wrong too. :crying:

  • Christian Buettner-167247

    SSChampion

    Points: 13729

    Hm, am I the only one who thinks the given answer is false?

    According to it, there will be a "type conversion". But instead there is only a "type conversion error". Two different things...

    Best Regards,

    Chris Bรผttner

Viewing 15 posts - 1 through 15 (of 25 total)

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