SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Try it out..


Try it out..

Author
Message
Ujar
Ujar
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 Visits: 102
Comments posted to this topic are about the item Try it out..
UMG Developer
UMG Developer
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4730 Visits: 2204
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.
VM-723206
VM-723206
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1148 Visits: 267
Nice Question.
tommyh
tommyh
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2854 Visits: 2000
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).
arun1_m1
arun1_m1
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1129 Visits: 612
Good Question. Thought both would truncate to 'GIV' and 'GIV'. Should have been more thoughtful.:-)
Duncan Pryde
Duncan Pryde
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4416 Visits: 1552
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
Dennissinned
Dennissinned
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1130 Visits: 289
Good question - Learned something new.
Daniel Bowlin
Daniel Bowlin
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10106 Visits: 2629
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.
Bradley Deem
Bradley Deem
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1119 Visits: 1248
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.
UMG Developer
UMG Developer
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4730 Visits: 2204
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...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search