Query using a derived table with ISNUMERIC results in conversion failure (varchar to int)

拈花ヽ惹草 提交于 2019-12-24 00:45:45

问题


Here's an example query:

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

If I have some varchar values and I limit them to numeric values using ISNUMERIC in a derived table in the query, why does it result in a conversion error?:

Conversion failed when converting the varchar value '134a' to data type int.

Is there a way around this?


回答1:


The WHERE clause executes first. Try:

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 


来源:https://stackoverflow.com/questions/9168597/query-using-a-derived-table-with-isnumeric-results-in-conversion-failure-varcha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!