我的查询
Select * from MyTable该表由300 k行组成。它为200k+行运行,并弹出此错误。

如何处理这些才能获得完整的数据?
MyTable有计算列吗?
表由一个名为IsExceeds的计算列组成,如下所示,供您参考。

这是计算列公式:
(CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))字段定义:
[Pro_PCT] [nvarchar](50) NULL,
[Max_Off] [nvarchar](50) NULL,
[IsExceeds] AS (CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))发布于 2019-03-14 08:36:36
请转换为浮点数,然后转换为int。
declare @n nvarchar(20)
set @n='11.11'
if (isnumeric(@n)=0)
SELECT 0
else
SELECT CAST(CONVERT(float, @n) as int) AS n发布于 2019-03-14 11:33:08
为什么要将金额存储为字符串?这是根本问题。
所以我建议修正你的数据。就像这样:
update mytable
set max_off = replace(max_off, '%');
alter table mytable alter pro_pct numeric(10, 4);
alter table mytable alter max_off numeric(10, 4);(没有样本数据或数据示例,我只是猜测一个合理的类型。)
然后,您可以将IsExceeds定义为:
(Pro_PCT - Max_Off)瞧!没问题。
发布于 2019-03-14 08:11:16
基于公式-- Pro_PCT或Max_Off包含值11.11 (嗯,还有一个额外的Max_Off % )。也许它们还包含其他不能转换为int的值。
下面是查找将导致此问题的所有行的方法:
Select *
from MyTable
where try_cast(Pro_PCT as int) is null
or try_cast(replace([Max_Off],'%','') as int) is null在找到它们之后,可以修复值,也可以将计算列的计算更改为使用try_cast或try_convert而不是convert。
https://stackoverflow.com/questions/55157257
复制相似问题