Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i work on sql server 2012 i face error
Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting int to data type numeric.
The statement has been terminated.

so how to solve this issue ?

statment generate error

What I have tried:

 select m.companyid,m.[Year],m.rev_id,CountMaterials,CountMaterialsConfirment,
FORMAT(case when c.CountMaterialsConfirment IS NULL then 0 ELSE cast(c.CountMaterialsConfirment as decimal(5,2)) / cast(m.CountMaterials as decimal(5,2))  END * 100,'N2')  as totalpercentage
into ExtractReports.dbo.CountTotalPercentage
 from ExtractReports.dbo.CountDistinctMaterials m with(nolock)
 left join ExtractReports.dbo.CountDistinctMaterialsConfirments c with(nolock) on m.companyid=c.companyid and m.[year]=c.[year] and m.rev_id=c.rev_id
Posted
Updated 1-Mar-22 19:07pm
v2

Are you sure that the highest value of CountMaterialsConfirment and CountMaterials field is 999.99?

Follow the instruction: Fix “Arithmetic overflow error converting int to data type numeric” in SQL Server[^]
 
Share this answer
 
v2
First thing check for empty string when and condition in CASE WHEN, Secondly check the value of CountMaterialsConfirment and CountMaterials, If its increasing then change decimal to (8,3) isn't going to do anything if it's greater than 99999.999 - you need to increase the number of digits before the decimal. Also try decimal(9,2) or decimal(10,2) or whatever according to your data value. Try the modified code:
FORMAT(CASE WHEN ((ISNULL(c.CountMaterialsConfirment, '') = '') AND (ISNULL(c.CountMaterials, '') = '')) THEN 0 
				ELSE (cast(c.CountMaterialsConfirment as decimal(9,2)) / cast(m.CountMaterials as decimal(9,2))) * 100 END,'N2')  as totalpercentage
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900