简体   繁体   English

将varchar转换为数值类型的算术溢出错误

[英]Arithmetic overflow error converting varchar to data type numeric

First, let me state I have read various similar posts and haven't been able to identify the similarities between the problem that other posters have had with this error message and the situation I've encountered. 首先,让我声明我已经阅读了各种类似的文章,还无法确定其他张贴者在此错误消息上遇到的问题与我遇到的情况之间的相似之处。 Perhaps I'm not searching correctly, but here's the scenario. 也许我没有正确搜索,但这是场景。 I'm trying to search for values in a table that are less than 70 when converted to a numeric value. 我正在尝试在表中搜索转换为数字值时小于70的值。 Sometimes the value can be stored with a comma (ie 3,080 etc.) so I have a replace statement to remove the comma. 有时,该值可以用逗号存储(例如3,080等),因此我有一个replace语句来删除逗号。 The obsValue column in the queries below is varchar(2000) and I'm guessing that may have something to do with it. 下面查询中的obsValue列是varchar(2000) ,我猜测可能与它有关。 My initial query worked: 我最初的查询工作:

Select name, obsValue
From database.dbo.table
Where name in ('LDL') 
and isnumeric(obsvalue) = 1 
and cast(replace(obsvalue,',','') as decimal(18)) < 70

This brings back expected values, but it's not the only name I'm trying to search for. 这会带回期望值,但这不是我要搜索的唯一name Other examples include ('LDL(CALC)') . 其他示例包括('LDL(CALC)') Using a UNION statement will allow me to union queries together but unfortunately I don't control the application code and this is not an option. 使用UNION语句将使我可以将查询合并在一起,但是不幸的是,我不控制应用程序代码,这不是一个选择。 The only option I have available is using an IN clause, so ultimately the query will look like this when I'm searching for a variety of name values: 我唯一可用的选项是使用IN子句,因此当我搜索各种name值时,最终查询将如下所示:

Select name, obsValue
From database.dbo.table
Where name in ('LDL', 'LDL(CALC)') 
and isnumeric(obsvalue) = 1 
and cast(replace(obsvalue,',','') as decimal(18)) < 70

And unfortunately doing it this way is where I get the error message. 不幸的是,这样做是我收到错误消息的地方。 I apologize if this has already been answered elsewhere. 抱歉,如果其他地方都没有回答。 Please link and I will give credit where credit is due. 请链接,我将在到期的信用额中给予信用。

There may be values in obsvalue that are too big for your cast( as decimal) function but you don't care about anyways because they don't meet your in() criteria. obsvalue中的某些值可能对您的cast(如十进制)函数而言太大,但您无论如何都不在乎,因为它们不符合in()标准。

Try applying the cast() in a subquery, limiting your query to obsvalues you actually need to convert. 尝试在子查询中应用cast(),将查询限制为实际需要转换的obsvalues。

Also, since the commas only exist if the value is greater than 999, and you're testing for values less than 70, you don't need the replace. 此外,由于逗号仅在值大于999时存在,并且您要测试的值小于70,因此不需要替换。 In fact, you could exclude any row containing a comma because you know it's too high. 实际上,您可以排除任何包含逗号的行,因为您知道它太高了。

The problem is that SQL Server is optimizing the query differently when there are multiple items in the IN clause. 问题在于,当IN子句中有多个项目时,SQL Server将优化查询的方式有所不同。 You can use a CASE statement to prevent the optimization 您可以使用CASE语句阻止优化

SELECT name, obsvalue
FROM database.dbo.table 
WHERE (CASE WHEN ( isnumeric(obsvalue) = 1 
             AND name in ('LDL', 'LDL(CALC)')) 
       THEN cast(replace(obsvalue,',','') as decimal(18))
       ELSE null END) < 70

You can replace the IsNumeric(obsvalue) with (select obsvalue where isnumeric(obsvalue) = 1). 您可以将IsNumeric(obsvalue)替换为(在isnumeric(obsvalue)= 1时选择obsvalue)。

Select name, obsValue
From database.dbo.table
Where name in ('LDL', 'LDL(CALC)') 
and isnumeric(obsvalue) = 1 
and cast(replace((select obsvalue where isnumeric(obsvalue) = 1),',','') as decimal(18)) < 70

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 算术溢出错误将varchar转换为数据类型numeric -error - Arithmetic overflow error converting varchar to data type numeric -error 错误:将数字转换为数据类型varchar的算术溢出错误 - Error : Arithmetic overflow error converting numeric to data type varchar 错误:将varchar转换为数值类型的算术溢出错误 - Error: Arithmetic overflow error converting varchar to data type numeric 将数字转换为数据类型varchar的算术溢出错误 - Arithmetic overflow error converting numeric to data type varchar 将varchar转换为数值类型-VBA的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric -VBA 在存储过程中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in stored procedure 将 varchar 转换为数据类型 numeric CASE 语句的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric CASE statement 将 varchar 转换为数据类型 numeric 的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric 将varchar转换为数值数据类型的算术溢出错误? - Arithmetic overflow error converting varchar to data type numeric? 在SQL Server中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM