Reputation: 731
I'm getting a Arithmetic overflow error converting varchar to data type numeric. In my stored procedure although only when it has to make the calculations. Here is my code, I'm using a convert still seems to be something wrong.
set @Text = @Text + 'Something:' +
(select case when s.ATF = '' then '0'
else Convert(varchar,100 *((s.ATF *1.00) /
(s.Total * 1.00))) + '%'
end
from Maintenance.dbo.stats s
where s.ID = @ID) + CHAR(13) + CHAR(10)
Thanx!
Upvotes: 0
Views: 1305
Reputation: 43023
The problem is that you take a varchar
and multiply it by a numeric value. That forces an implicit conversion to a numeric type. You should specify conversion explicitly. I chose decimal(38,6)
but you can change that.
set @Text = @Text + 'Something:' +
(select case when s.ATF = '' then '0'
else Convert(varchar, 100 * cast(s.ATF as decimal(38,6)) /
cast(s.Total as decimal(38,6))) + '%'
end
from Maintenance.dbo.stats s
where s.ID = @ID) + CHAR(13) + CHAR(10)
You can see how this wrong conversion works on that simple example:
declare @x as varchar(50) = '11'
select @x * 1.00
Upvotes: 1