2

这是一个示例查询:

DECLARE @table table (loc varchar(10))

INSERT INTO @table VALUES
('134a'), ('123'), ('abc'), ('124')

SELECT * 
FROM (
    SELECT * FROM @table WHERE ISNUMERIC(loc) = 1
) as a
WHERE CAST(loc as INT) BETWEEN 100 AND 200

如果我有一些 varchar 值,并且我将它们限制为在查询的派生表中使用的数值ISNUMERIC,为什么会导致转换错误?:

将 varchar 值“134a”转换为数据类型 int 时转换失败。

有没有解决的办法?

4

1 回答 1

5

WHERE子句首先执行。尝试:

DECLARE @table table (loc varchar(10)) 

INSERT INTO @table VALUES 
('134a'), ('123'), ('abc'), ('124') 

SELECT *  
FROM ( 
    SELECT * FROM @table
) as a 
WHERE ISNUMERIC(loc) = 1 and CAST(loc as INT) BETWEEN 100 AND 200 
于 2012-02-07T03:49:59.170 回答