Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Conversion Failed varchar to int

Author  Topic 

rosshughes
Starting Member

16 Posts

Posted - 2011-07-12 : 16:40:35
Hi All,

I am having trouble getting the following to work.

opqtycompleted int,
standardhrs varchar(50),
laborstd varchar(50)

I insert values in to this memory table and all works fine. While I extract data from the memory table, I have one field in a query that looks like this:

CAST((CASE WHEN CAST(IsNull(standardhrs,0) as Int) <> 0 OR IsNull(opqtycompleted,0) <> 0 THEN 1/(standardhrs/opqtycompleted) ELSE standardhrs END) As varchar(50)) As laborstd

When this conditional field is included in the query I get the following error:

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

The laborstd field is varchar and I am converting the resulting CASE statement results to varchar.

What am I missing? Where is the int coming from?

Thanks,
Ross

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-12 : 16:45:13
I suspect the problem is here: CAST(IsNull(standardhrs,0) as Int)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rosshughes
Starting Member

16 Posts

Posted - 2011-07-12 : 16:51:25
Thanks for the quick response.

I did try it the other way at first and didn't have any luck.

I tried this again:

CAST((CASE WHEN IsNull(standardhrs,0) <> 0 OR IsNull(opqtycompleted,0) <> 0 THEN 1/(standardhrs/opqtycompleted) ELSE standardhrs END) As varchar(50)) As laborstd

...and got the same result. Unless you had something else in mind?

Thanks Tara,
Ross
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-12 : 17:35:49
I think you'll need to cast opqtycompleted to varchar for this to work.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rosshughes
Starting Member

16 Posts

Posted - 2011-07-12 : 17:46:36
Thanks. I did that too and then it gives me a "can't divide by a varchar" error.

It has me puzzled. Is there something implicit that converts datatypes because I am dividing into 1? I tried to CAST that too and can't get it to work.

I will keep working at it.

Thanks,
Ross
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-12 : 17:54:06
Can you instead convert your data into the proper data type, which appears to be decimal? Doing math on data should be done on numerical data types.

Yes implicit conversions are occurring.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-07-12 : 18:15:51
Your standard hours may not be ints. They may actually be like 1.23 or .7. You'll need to convert standardhours to a proper type -- numeric(p,s) as Tara suggested -- everywhere it is used in your query. Also, each part of your case statment needs to return the same data type throughout. The THEN part of your case expression returns an int, but the THEN part returns a varchar(50).

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

rosshughes
Starting Member

16 Posts

Posted - 2011-07-13 : 09:39:11
Thank you both for your help. I went back to my declarations and started with everything numeric and then cast back to varchar at the very end. That solved it.

Ross
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-13 : 12:43:06


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -