|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, October 12, 2010 4:24 AM
Points: 41,
Visits: 102
|
|
| Comments posted to this topic are about the item Try it out..
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
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.
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Saturday, April 06, 2013 12:20 AM
Points: 649,
Visits: 263
|
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:15 AM
Points: 1,476,
Visits: 1,943
|
|
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 ).
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 12:25 PM
Points: 811,
Visits: 440
|
|
Good Question. Thought both would truncate to 'GIV' and 'GIV'. Should have been more thoughtful.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 2:03 AM
Points: 3,046,
Visits: 1,307
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 19, 2011 7:26 AM
Points: 1,078,
Visits: 289
|
|
| Good question - Learned something new.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 1:10 PM
Points: 2,673,
Visits: 2,418
|
|
| 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.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 9:22 AM
Points: 551,
Visits: 1,150
|
|
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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
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...
|
|
|
|