T-SQL

  • This is of course a limitation with the ISNULL() function. The datatype and in this case str1 is varchar(5) meaning that the replacing statement will be confined to 5 characters. Whereas COALESCE() is not dependent in this way but will return the first not null value of whatever datatype it may be. COALESCE() is the better choice.

  • tjskidmore (12/19/2014)


    This is of course a limitation with the ISNULL() function. The datatype and in this case str1 is varchar(5) meaning that the replacing statement will be confined to 5 characters. Whereas COALESCE() is not dependent in this way but will return the first not null value of whatever datatype it may be. COALESCE() is the better choice.

    Not quite so. COALESCE uses the data type with the highest data type precedence which can generate unexpected errors if you're not aware of this.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Good question, though not the first time I have seen it.

    The explanation can be improved. What happens is that ISNULL is defined as returning the same data type as the first argument. Whereas the (otherwise mostly similar, but more portable and more flexible) alternative keyword COALESCE uses the standard rules of data type preference between all arguments passed in.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • SALIM ALI (12/19/2014)


    the answer is only correct when using SqlServer 2012 and above. Previous versions prompt an error.

    That`s not correct, as SQL 2k8 will work fine with the in-line assignement.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Nice question, thanks.

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

  • A nice question. But a shame that 'SQL String' wasn't an option, and that there was no reference as to what happens when you assign a value longer than the definition (ie it truncates it rather than raising an error).

  • Very Tricky.. Learning Lot.

  • Thanks for the question.

  • Toreador (12/22/2014)


    A nice question. But a shame that 'SQL String' wasn't an option, and that there was no reference as to what happens when you assign a value longer than the definition (ie it truncates it rather than raising an error).

    +1

    Literally exactly what I was going to say.

Viewing 9 posts - 16 through 23 (of 23 total)

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