Joshua Lockhart
Joshua Lockhart

Reputation: 35

Arithmetic overflow error converting nvarchar to data type numeric when creating string

Trying to figure out why I am getting this error that happens when creating a string to exec

SET @SQLINSERTSTRINGFINAL = @SQLINSERTSTRING + @Suggested_Qty+''','''+
    @Required_Qty+''','''+
    @System_Cost+''','''+
    @Revised_Cost

These 4 variables are populated from

convert(numeric(7,2),ltrim(rtrim(floor((case when r.calc_qty > 99999 then 99999 else r.calc_qty end)/(case when s.PLBCF_1 is null or s.PLBCF_1 =0 then 1 else s.PLBCF_1 end))))),
    convert(numeric(7,2),ltrim(rtrim(floor((case when r.po_number <> 'NONE' then r.calc_qty else case when r.adj_qty > 99999 then 99999 else r.adj_qty end end)/(case when s.PLBCF_1 is null or s.PLBCF_1 = 0 then 1 else s.PLBCF_1 end))))), 
    convert(numeric(9,4),ltrim(rtrim(case when s.std_cost is null then r.std_cost else s.std_cost end))),
    convert(numeric(9,4),ltrim(rtrim(r.std_cost)))

I've check every table used and they are consistent in the column type. Only non numeric in there is r.po_number which can be of value 'NONE'

Upvotes: 2

Views: 5959

Answers (1)

Derek
Derek

Reputation: 23258

You need to convert each numeric to a string when you're setting the final string value like this:

SET @SQLINSERTSTRINGFINAL = @SQLINSERTSTRING + convert(nvarchar(max), @Suggested_Qty)+''','''+
    convert(nvarchar(max), @Required_Qty)+''','''+
    convert(nvarchar(max), @System_Cost)+''','''+
    convert(nvarchar(max), @Revised_Cost)

It won't auto-cast for you

Upvotes: 1

Related Questions