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


PRINT vagaries


PRINT vagaries

Author
Message
hrvoje.piasevoli
hrvoje.piasevoli
SSC-Addicted
SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)

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

Hrvoje

Hrvoje Piasevoli
cengland0
cengland0
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1692 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
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11242 Visits: 12005
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 (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

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

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

Group: General Forum Members
Points: 44 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
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29917 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
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3633 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 (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
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
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 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