December 19, 2014 at 10:59 am
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.
December 19, 2014 at 12:37 pm
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.
December 20, 2014 at 1:08 pm
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.
December 20, 2014 at 10:54 pm
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
December 22, 2014 at 12:51 am
Nice question, thanks.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
December 22, 2014 at 2:55 am
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).
December 22, 2014 at 6:30 am
Very Tricky.. Learning Lot.
December 22, 2014 at 9:32 am
Thanks for the question.
December 23, 2014 at 8:30 am
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