Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Implicit Conversions Expand / Collapse
Author
Message
Posted Monday, May 05, 2008 8:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 17, 2008 10:03 AM
Points: 1, Visits: 20
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.
Post #495001
Posted Monday, May 05, 2008 8:25 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: 2 days ago @ 1:34 PM
Points: 15,442, Visits: 9,588
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
Post #495007
Posted Monday, May 05, 2008 8:29 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 1:49 PM
Points: 32,768, Visits: 14,929
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
Post #495009
Posted Monday, May 05, 2008 8:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:08 PM
Points: 5,469, Visits: 23,464
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
Post #495034
Posted Monday, May 05, 2008 9:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:08 PM
Points: 5,469, Visits: 23,464
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
Post #495051
Posted Monday, May 05, 2008 12:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, April 15, 2010 9:38 AM
Points: 12, Visits: 26
This will not compile in SQL 2005....

Conversion failed when converting the varchar value 'B' to data type int.
Post #495173
Posted Monday, May 05, 2008 1:27 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, August 13, 2013 11:43 AM
Points: 242, 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.
Post #495220
Posted Monday, May 05, 2008 4:13 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, February 28, 2014 11:02 AM
Points: 46, Visits: 177
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?
Post #495332
Posted Tuesday, May 06, 2008 3:48 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:43 AM
Points: 2,845, Visits: 4,830
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



Post #495470
Posted Tuesday, May 06, 2008 9:13 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 8:55 AM
Points: 1,520, Visits: 3,035
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

Post #495726
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse