Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


PRINT vagaries


PRINT vagaries

Author
Message
hrvoje.piasevoli
hrvoje.piasevoli
Old Hand
Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)

Group: General Forum Members
Points: 365 Visits: 510
Good tricky one...thank you
It would be even trickier if the first answer was 5.

Hrvoje

Hrvoje Piasevoli
cengland0
cengland0
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 1300
info 25880 (6/30/2010)
Yes, that's reason, because the result of an implicit casting of '-' is zero
SELECT 6 + 1 + '-'



So if you substitute the '-' with something clearly a string like 'a', you get a conversion failed message.
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8339 Visits: 11584
Good question. The only thing I dislike about it is the description "PRINT vagaries" because, as others have noted, the PRINT statement is completely innocent.

A better name would have been "Implicit type casting vagaries".


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
TonyThorne
TonyThorne
SSC-Addicted
SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)

Group: General Forum Members
Points: 455 Visits: 24
Nice question. At first glance my answer would have been 5, but as this was not an option, I had to review the question.

I think more people would have been caught out if 5 had been an answer.
prashant.bhatt
prashant.bhatt
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 177
Excellent question and great to know the deal

Prashant Bhatt
Sr Engineer - Application Programming
kimetch
kimetch
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 87
I was surprised to find that '-' converts to 0. I expected the print statement would have producted "Conversion failed when converting the varchar value '-' to data type int." You could've tricked us by putting that as one of the choices.
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20941 Visits: 9671
Hehe, got me too. Never dreamed that '-' and '+' converts to 0. Tho it makes perfect sens when you think about it!
Christian Buettner-167247
Christian Buettner-167247
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2951 Visits: 3889
kimetch (6/30/2010)
I was surprised to find that '-' converts to 0. I expected the print statement would have producted "Conversion failed when converting the varchar value '-' to data type int." You could've tricked us by putting that as one of the choices.

Interestingly, you get an overflow when working with decimal instead:
SELECT CAST('-' AS decimal(38,19))



Best Regards,

Chris Büttner
Bob Razumich
Bob Razumich
Mr or Mrs. 500
Mr or Mrs. 500 (516 reputation)Mr or Mrs. 500 (516 reputation)Mr or Mrs. 500 (516 reputation)Mr or Mrs. 500 (516 reputation)Mr or Mrs. 500 (516 reputation)Mr or Mrs. 500 (516 reputation)Mr or Mrs. 500 (516 reputation)Mr or Mrs. 500 (516 reputation)

Group: General Forum Members
Points: 516 Visits: 1153
cengland0 (6/30/2010)
info 25880 (6/30/2010)
Yes, that's reason, because the result of an implicit casting of '-' is zero
SELECT 6 + 1 + '-'



So if you substitute the '-' with something clearly a string like 'a', you get a conversion failed message.


My first thought was that the correct answer would have been the conversion failed because of the dash. When it was not in the options of possible answers, I guessed incorrectly with the 6-1. Always nice to learn something new.
lbrigham
lbrigham
Mr or Mrs. 500
Mr or Mrs. 500 (599 reputation)Mr or Mrs. 500 (599 reputation)Mr or Mrs. 500 (599 reputation)Mr or Mrs. 500 (599 reputation)Mr or Mrs. 500 (599 reputation)Mr or Mrs. 500 (599 reputation)Mr or Mrs. 500 (599 reputation)Mr or Mrs. 500 (599 reputation)

Group: General Forum Members
Points: 599 Visits: 640
I agree with the posts saying that '5' should've been an option as I would've definitely chose that. When I first read through the answer list, and saw that 5 wasn't an option, I thought maybe it was printing the values as strings so chose 6-1, but understand now after reading the description that the math ends up being 6+0+1.

Great question - learned something.
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