Error Converting Varchar To INT Inline Error

How can I avoid this error so that my sql will execute properly/? Below is my code/ddl.

	Create Table DZ 
	(
		date datetime
	)
	Create Table GBCD
	(
		salesmanName varchar(100),
		TimeBilled decimal(18,2),
	)
	
INSERT INTO GBCD VALUES
('No Way', 15.30),
('FallOut', 12.16)

Declare @count int
set @count = (select count(*) from DZ)
Declare @sql2 varchar(1000)
set @sql2 = 'Select '
+',case when TimeBilled > 0 then convert(decimal(18,2),convert(decimal(18,2),TimeBilled)/convert(decimal(18,2),'+@count+')) else 0 end AS [TB] '
+'FROM GBCD a '
exec @sql2

And this is the error I get:

Msg 245, Level 16, State 1, Line 18
Conversion failed when converting the varchar value 'Select ,case when TimeBilled > 0 then convert(decimal(18,2),convert(decimal(18,2),TimeBilled)/convert(decimal(18,2),' to data type int.

September 13th, 2015 8:29pm

Hi HotWildWetSlides,

Please see the errors and correction in your code.

Declare @count int
set @count = (select count(*) from DZ)

--if @count<>0, it would return Divide by zero error
IF @count<>0
	BEGIN
		Declare @sql2 varchar(1000)
		set @sql2 = 'Select '
		--+',case when TimeBilled > 0 then convert(decimal(18,2),convert(decimal(18,2),TimeBilled)/convert(decimal(18,2),'+@count+')) else 0 end AS [TB] '
		--remove the leading comma
		--cast @count as varchar
		+'case when TimeBilled > 0 then convert(decimal(18,2),convert(decimal(18,2),TimeBilled)/convert(decimal(18,2),'+CAST(@count AS VARCHAR(20))+')) else 0 end AS [TB] '
		+'FROM GBCD a '; 
		--PRINT @SQL2
		EXEC @SQL2		
	END
	 

If you have any question, feel free to let me know
Free Windows Admin Tool Kit Click here and download it now
September 13th, 2015 9:14pm

Thank you for trying to post DDL. It has syntax errors. A table has to have a key by definition, so these are "piles", a slang term in RDBMS that usually has a bad adjective in front of it. 

Then you failed to follow ISO-1179 rules. DATE is not a valid column name! The term datetime is an old Sybase dialect that we call TIMESTAMP in ANSI/ISO Standard SQL. I will assume the DZ is a standard, precise term in your industry. Here is the only way you could have declared your first table: 

CREATE TABLE DZ 
(dz_timestamp DATETIME2(0) NO NULL PRIMARY KEY); 

Your next (non-)table is a mess, too. Again I will assume that GBCD is a standard, precise term in your industry. Do you really have people which such a long name? The post Office says that all you need is 35 characters. What careful research did you do for this? What is the unit of measurement for the billable time? I will guess decimal hours, since that is common in many industries. You also failed to post sample data for that table. 

CREATE TABLE GBCD
(salesman_name VARCHAR(35) NOT NULL PRIMARY KEY,
 billable_time DECIMAL(18,2) NOT NULL);
 
INSERT INTO GBCD 
VALUES ('NO WAY', 15.30), ('FALLOUT', 12.16);

What you do next is awful in so many ways. Since SQL is declarative, we do not use local variables. We nest expressions! 

CONVERT() is an old Sybase display formatting function; SQL programmers use CAST(). But then we see that you are casting a column back to its original data type! Why? 

Dynamic SQL is an admission of failure. You tell the world that your code is so bad that you (or a random unknown user) have to repair it at run time. Here is my guess: 

SELECT CASE WHEN billable_time > 0.00 
       THEN billable_time/(SELECT COUNT(*) FROM DZ))
       ELSE 0.00 END AS daily_billable_time
  FROM GBCD; 

But the computation makes no sense. There is no relationship between the tables! Want to try again? 
September 13th, 2015 10:15pm

You avoid it by not using dynamic SQL when you don't need to.

Select case when TimeBilled > 0 then convert(decimal(18,2),convert(decimal(18,2),TimeBilled)/convert(decimal(18,2),
(select count(*) from DZ)
)) else 0 end AS [TB]
FROM GBCD a

And if you really want to use dynamic SQL - which is a serious mistake when there is no reason for it - you should use a parameterised statement:

Declare @count int
set @count = (select count(*) from DZ)
Declare @sql2 nvarchar(1000)
set @sql2 = 'Select case when TimeBilled > 0 then convert(decimal(18,2),convert(decimal(18,2),TimeBilled)/convert(decimal(18,2), @count)) else 0 end AS [TB] '
+ 'FROM GBCD a '
EXEC sp_executesql @sql2, N'@count int', @count

This makes the dynamic SQL easier to compose.

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 2:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics