user1794118
user1794118

Reputation: 21

SQL Server 2005 : conversion failed when converting the varchar value '0.00' to data type int

When adding this line into my query:

convert(varchar(20), convert(varchar(20),
sum(case when tsr.other like '%aa%' then tsr.block1 else 0 end) +
sum(case when tsr.other like '%aa%' then tsr.block2 else 0 end) +
sum(case when tsr.other like '%aa%' then tsr.block3 else 0 end) +
sum(case when tsr.other like '%aa%' then tsr.block4 else 0 end)) * 450) 

I get this error message:

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

Data in block column is days - e.g. 10.0

Any ideas?

i've got it fixed, it was just changing the 450 to 450.0.

The reason for the varchars is that this is just 1 line from 1 of multiple unioned select statements.

Upvotes: 2

Views: 1220

Answers (2)

Shankar Kamble
Shankar Kamble

Reputation: 3023

 '10.0' isn't an int/decimal  - it's a varchar .   
 do any mathematical calculation only on decimal/numeric/float/int values.

 SELECT convert(varchar(20), convert(decimal(10,2), sum(case when
 tsr.other like '%aa%' then  convert(decimal(10,2),tsr.block1) else 0
 end) + sum(case when tsr.other like '%aa%' then 
 convert(decimal(10,2),tsr.block2) else 0 end) + sum(case when
 tsr.other like '%aa%' then  convert(decimal(10,2),tsr.block3) else 0
 end) + sum(case when tsr.other like '%aa%' then 
 convert(decimal(10,2),tsr.block4) else 0 end)) * 450)

Upvotes: 1

podiluska
podiluska

Reputation: 51494

10.0 isn't an int - it's a decimal.

Try

declare @i int 
select @i = convert(decimal(9,4),'10.0')
select @i

and the conversion from the decimal to the int will be done implicitly.

Upvotes: 1

Related Questions