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


Implicit Conversions


Implicit Conversions

Author
Message
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23709 Visits: 9730
bvesperman (5/5/2008)
I agree that this doesn't work. I was pretty sure it wasn't going to do an implicit conversion. Then I cut and pasted it into a query and executed it. And I was proven right. It failed. No results were returned. An error was returned. It things like this that makes me not try to read or follow information from sites like this one. Is there a setting in MSSQL that allows implicit conversion? I hope not. My databases need to maintain data integrity, and if implicit conversions are happening I can't trust my databases.


Yes, implicit conversions happen.

It's not a problem, if you know how they work. Books Online has exact data on what they do and why and how.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Steve Jones
Steve Jones
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: Administrators
Points: 63310 Visits: 19115
That is not a compilation error. It's a run-time error when a conversion occurs. There are very few compilation errors, mostly syntax issues.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7869 Visits: 25280
AS previously stated:
dunnjoe (5/5/2008)
That's why I feel answer b is not correct. Answer b says the value is converted to an int. The error message says the conversion failed. So, it appears to be a compilation error.


Unfortunately I came to the same conclusion ... the statement did NOT execute ... other than the error message
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'B' to data type int.


There were NO values returned!!

Still a good QOD just a bad wording of possible answers ... to the detriment of many of us

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7869 Visits: 25280
Steve Jones - Editor (5/5/2008)
That is not a compilation error. It's a run-time error when a conversion occurs. There are very few compilation errors, mostly syntax issues.

In my humble opinion the answer
The varchar is converted in int (error results)
should have been

The varchar is not converted into int (run time error results)

Since this union statement will work (no run time error)

select '10'
Union
Select 4

and this will compile

CREATE PROCEDURE Dbo.Oops
AS
select 'B'
Union
Select 4

but will result in the run time error when invoked.

Oh well looking back now .... I have gained knowledge from the QOD and that is its objective.

Thanks Steve for making think even more

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Trace Herrell-425305
Trace Herrell-425305
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 26
This will not compile in SQL 2005....

Conversion failed when converting the varchar value 'B' to data type int.
srienstr
srienstr
Old Hand
Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)

Group: General Forum Members
Points: 332 Visits: 433
Trace Herrell (5/5/2008)
This will not compile in SQL 2005....

Conversion failed when converting the varchar value 'B' to data type int.


On the contrary, it did compile but it encountered an error when it ran. If you mistyped and put in "Skelect 'B' ..." it would not compile. Conversion failure is a runtime error, in this case because a varchar 'B' can't be converted to int. The compiler doesn't check to make sure you won't run into data type errors.


Puto me cogitare, ergo puto me esse.
I think that I think, therefore I think that I am.
Sponge-592981
Sponge-592981
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 223
I feel the same way, in addition note the wording:

This is an example of an implicit conversion. In this case, an error is returned as the varchar is converted to an int, or an attempt is made, which returns an error. The int is of higher precedence than a varchar, so that is the order of conversions.

What is it converted and when error says failed to convert and then you say varchar IS converted to an int and then you say OR attempt is made?

Which one is it?
rodjkidd
rodjkidd
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4273 Visits: 8412
That will teach me for speed reading the answers - missed the (error results) at the end of answer two! So picked answer 3! My bad...
(But at least I knew it wouldn't work!)

Rodney



john.arnott
john.arnott
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1990 Visits: 3059
Excellent question in that to understand the result, you have to step through the entire process.
--The statements compile.
--Each "select" is executed.
--Then the two result sets are combined to effect the Union. That's where the error occurs.

That's why even though the alpha character "B" was earlier in the code, the execution tried to convert it to the higher precedence integer type to satisfy the column type forced by the later value "4".

The compiler couldn't catch this as it won't look at the values, only the datatypes. If the character input had been a digit (in quotes), it would run. This works:

select '3'
union
select 4


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: 3595 Visits: 3889
Hm, the answer is kind of paradox.

Either the "the varchar is converted to int" or an "error occurs". But not both.
I would have preferred an answer like this:
An error occurs during conversion from varchar to int.

But i should mostly blame myself for not reading the answer to the end...

Best Regards,

Chris Büttner
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