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
SSC Veteran
SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)

Group: General Forum Members
Points: 232 Visits: 6
Comments posted to this topic are about the item Char with null
Russell Shilling
Russell Shilling
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1039 Visits: 209
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
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3918 Visits: 1502
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 Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

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

Group: General Forum Members
Points: 5079 Visits: 3327
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.

I run on tuttopodismo
Ed Wagner
Ed Wagner
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17105 Visits: 10094
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
Mr or Mrs. 500
Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)Mr or Mrs. 500 (586 reputation)

Group: General Forum Members
Points: 586 Visits: 1187
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
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14152 Visits: 885
I've been caught by these before so had to try it out.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26844 Visits: 17557
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
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7483 Visits: 4865
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