Try it out..

  • Comments posted to this topic are about the item Try it out..

  • Nice question thanks!

    Though instead of your explanation I mights say "While ISNULL returns that value with the type of the first parameter COALESCE returns the value with they type of the actual value being returned."

    So it would be perfectly reasonable for a single COALESCE statement to return 5 or more different types depending on the inputs. (Well maybe not reasonable, but certainly possible.)

    On the other hand a given ISNULL statement will always return the same type.

  • Nice Question.

  • Good question. However an option with "GIVEN STRING IS NULL, GIV" would have been good. As the question stands its rather easy to guess. An inverted correct answer would have made it harder for people guessing (like me :-D).

  • Good Question. Thought both would truncate to 'GIV' and 'GIV'. Should have been more thoughtful.:-)

  • Good question - it was obvious immediately what was being asked, which is always nice (I always have a problem with obscure/trick questions!).

    Also, it's nice to be reminded of these things (I rarely use ISNULL - perhaps this is why.)

    Duncan

  • Good question - Learned something new.

  • Great question. I love QoTDs like this. They make me think and maybe read a little to make sure I understand something. Best of all QoTD is definitely making me better at all aspects of SQL Server. It is a great way to start the day and continually test and expand my knowledge.

  • UMG Developer (8/3/2010)


    So it would be perfectly reasonable for a single COALESCE statement to return 5 or more different types depending on the inputs. (Well maybe not reasonable, but certainly possible.)

    Actually, with Coalesce it comes down to the expression with the highest data type precedence being returned, thus exactly 1 predictable type will be returned. Consider the following which results in an error.

    DECLARE @STR int

    SET @STR=NULL

    SELECT COALESCE(@STR,'GIVEN STRING IS NULL',CAST(1 AS DATETIME))

    Conversion failed when converting date and/or time from character string.

    In terms of precedence, Datetime > Int > String, thus the int can be cast into a DateTime, but the String cannot, hence the error.

  • Bradley Deem (8/4/2010)[hrActually, with Coalesce it comes down to the expression with the highest data type precedence being returned, thus exactly 1 predictable type will be returned. Consider the following which results in an error.

    Argh... Thanks for the correction, I knew that but for whatever reason wasn't thinking straight...

  • Thanks for the question.

    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

  • Good Question.:-)

  • nice info about isnull and coalesce... thanks 🙂

  • Very nice question.

    Fortunately for me, I've come to know the differences of both functions while studying for the MCTS exam.

    The best of all are these discussions about the question of the day.

    You always get to learn something new and read the opinions of more qualified professionals than me. 🙂

    Best regards,

    Andre Guerreiro Neto

    Database Analyst
    http://www.softplan.com.br
    MCITPx1/MCTSx2/MCSE/MCSA

  • Hi,

    Thanks for posting this. I have few clarification on COALESCE result type and i am confused the type conversion.

    Consider the case below.

    --Case 1

    DECLARE @STR int

    SET @STR=NULL

    SELECT COALESCE(@STR,null,CAST('2010-05-05' AS DATETIME))

    Result: 2010-05-05 00:00:00.000

    --Case 2

    DECLARE @STR2 int

    SET @STR2=NULL

    SELECT COALESCE(@STR2,null,CAST('ABCD' AS varchar(10)))

    Result:

    Error: Msg 245, Level 16, State 1, Line 9

    Conversion failed when converting the varchar value 'ABCD' to data type int.

    --Case 3

    DECLARE @STR2 int

    SET @STR2=NULL

    SELECT COALESCE(@STR2,null,CAST('2222' AS varchar(10)))

    Result: 2222

    In case1, @STR is int type, but expression 2 in COALESCE is datetime. But i except the output to be 40301 which is the int value for date 2010-05-05 when following rule on case2 and case 3?

    How case1 is evaluated?

    Also what exactly mean by "1. Data type determination of the resulting expression - ISNULL uses the first parameter type, COALESCE follows the CASE expression rules and returns type of value with highest precedence"

    Thanks in advance

    Gopi

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

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