[英]Arithmetic overflow error converting varchar to data type numeric?
Just now I was getting this error when running a stored procedure: 刚才我在运行存储过程时遇到此错误:
Arithmetic overflow error converting varchar to data type numeric.
I located the line where that error was coming from and this is the code on that line: 我找到了该错误发生的行,这是该行的代码:
SELECT @AF_MIN_3L = LEFT(MIN([A-F Est_CY]), 6) - 0.000001 FROM #Ent_AF_3
Earlier in the stored procedure, I declared @AF_MIN_3L as data type FLOAT, created the temp table #Ent_AF_3 and in doing so, made the column [AF Est_CY] data type FLOAT. 在存储过程的前面,我将@ AF_MIN_3L声明为数据类型FLOAT,创建了临时表#Ent_AF_3,并在此过程中使列[AF Est_CY]数据类型为FLOAT。 Is the following code creating a non-FLOAT value? 以下代码是否创建非FLOAT值?
LEFT(MIN([A-F Est_CY]), 6) - 0.000001
I hope it's a simple casting issue and all I have to do is something like this: 我希望这是一个简单的转换问题,我要做的只是这样:
LEFT(MIN(CAST([A-F Est_CY] AS FLOAT)), 6) - CAST(0.000001 AS FLOAT)
I didn't want to run the whole procedure again without being sure I fixed the issue. 在不想确定已解决此问题的情况下,我不想再次运行整个过程。 Thanks for any help. 谢谢你的帮助。
If you use a string function, which is what LEFT
is, it resturns a string value. 如果您使用的是字符串函数(即LEFT
),它将返回一个字符串值。 As described here , the return datatype of the LEFT
function does indeed return a VARCHAR
or NVARCHAR
. 如上所述这里时,返回的数据类型LEFT
函数确实返回VARCHAR
或NVARCHAR
。 So, ideed, a CAST
or CONVERT
back to FLOAT
is required. 因此,实际上,需要CAST
或CONVERT
返回FLOAT
。 You should of course convert back to FLOAT
AFTER the LEFT
function, so it would be: CAST(LEFT(...) as FLOAT). 当然,您应该在LEFT
函数之后将其转换回FLOAT
,因此它将是:CAST(LEFT(...)as FLOAT)。
The problem is your precision - if you have a float > 0.999999 on the temp file you will get the error because of the implicit conversion. 问题是您的精度-如果您在临时文件上的浮点数> 0.999999,则由于隐式转换,将出现错误。
Use: 采用:
SELECT CAST(LEFT(MIN([A-F Est_CY]), 6) AS float) - 0.000001 FROM #Ent_AF_4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.