简体   繁体   中英

Arithmetic overflow error converting varchar to data type numeric in stored procedure

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM