由于您的列包含混合信息并且转换应用于整个列,因此您需要确保仅尝试转换实际数值:
select *
from Exam
where case when ISNUMERIC(sub1) =1
and CHARINDEX('.', sub1) = 0
and sub1 >= -2147483648
and sub1 <= 2147483647
then cast(sub1 as int)
else 0
end > 81
这是一个关于如何在 SQL Server 中执行此操作的示例。
这个查询还有一个注意点,那就是“全球化”。这 '。'我们正在检查,因为小数点并非到处都用作小数点。如果您有本地化的 SQL Server,您可以通过
获取正确的小数分隔符
DECLARE @decimal_separator char(1)
set @decimal_separator = SUBSTRING(CONVERT(CHAR(3), CONVERT(NUMERIC(2,1), 1.0/2)), 2, 1)
这会将查询变成:
DECLARE @decimal_separator char(1)
set @decimal_separator = SUBSTRING(CONVERT(CHAR(3), CONVERT(NUMERIC(2,1), 1.0/2)), 2, 1)
select *
from Exam
where case when ISNUMERIC(sub1) =1
and CHARINDEX(@decimal_separator, sub1) = 0
and sub1 >= -2147483648
and sub1 <= 2147483647
then cast(sub1 as int)
else 0
end > 81