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


Char with null


Char with null

Author
Message
tejinder.barnala
tejinder.barnala
Old Hand
Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)Old Hand (314 reputation)

Group: General Forum Members
Points: 314 Visits: 6
Comments posted to this topic are about the item Char with null
Russell Shilling
Russell Shilling
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2420 Visits: 222
The answer is clearly wrong. Some of these questions are really questionable.

The
isnull(left(@b,3),'I ô Char') as ba
will also return the string. The left function of @b returns NULL.

So long, and thanks for all the fish,

Russell Shilling, MCDBA, MCSA 2K3, MCSE 2K3
Rune Bivrin
Rune Bivrin
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5279 Visits: 1699
Russell Shilling (1/10/2017)
The answer is clearly wrong. Some of these questions are really questionable.

The
isnull(left(@b,3),'I ô Char') as ba
will also return the string. The left function of @b returns NULL.

Well, if you'd run the code you'd find it's completely correct.
What's missing in the explanation is the behaviour of ISNULL(). It always derives its return type from the first argument, which in the "ba" case will be char(3) as that is what LEFT() will return.


Just because you're right doesn't mean everybody else is wrong.
sendijunk
sendijunk
SSC-Addicted
SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)SSC-Addicted (402 reputation)

Group: General Forum Members
Points: 402 Visits: 111
Good question. Could use a bit more explanation though. Interesting what happens if you replace ISNULL with COALESCE.
Carlo Romagnano
Carlo Romagnano
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12418 Visits: 3517
sendijunk (1/11/2017)
Good question. Could use a bit more explanation though. Interesting what happens if you replace ISNULL with COALESCE.

The return type depends on "precedence" of types. In this case, the longest string of 8 chars.
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (158K reputation)SSC Guru (158K reputation)SSC Guru (158K reputation)SSC Guru (158K reputation)SSC Guru (158K reputation)SSC Guru (158K reputation)SSC Guru (158K reputation)SSC Guru (158K reputation)

Group: General Forum Members
Points: 158520 Visits: 11666
And here I was looking for the trick. ;-) Thanks.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Bob Razumich
Bob Razumich
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1204 Visits: 1277
Rune Bivrin (1/10/2017)
Russell Shilling (1/10/2017)
The answer is clearly wrong. Some of these questions are really questionable.

The
isnull(left(@b,3),'I ô Char') as ba
will also return the string. The left function of @b returns NULL.

Well, if you'd run the code you'd find it's completely correct.
What's missing in the explanation is the behaviour of ISNULL(). It always derives its return type from the first argument, which in the "ba" case will be char(3) as that is what LEFT() will return.


And for those inquiring minds that would like a quick link for the ISNULL function, https://msdn.microsoft.com/en-us/library/ms184325.aspx.
Iwas Bornready
Iwas Bornready
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38948 Visits: 886
I've been caught by these before so had to try it out.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149933 Visits: 18581
Rune Bivrin (1/10/2017)
Russell Shilling (1/10/2017)
The answer is clearly wrong. Some of these questions are really questionable.

The
isnull(left(@b,3),'I ô Char') as ba
will also return the string. The left function of @b returns NULL.

Well, if you'd run the code you'd find it's completely correct.
What's missing in the explanation is the behaviour of ISNULL(). It always derives its return type from the first argument, which in the "ba" case will be char(3) as that is what LEFT() will return.


+1

The question itself I thought was a great question but the explanation skipped the important part about the return datatype from ISNULL.

I look forward to the next question from Tejinder and hope the explanation is as good as the question next time.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Revenant
Revenant
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22605 Visits: 5137
Iwas Bornready (1/11/2017)
I've been caught by these before so had to try it out.
Me, too.

Thanks, Tejinder!
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