SQL 将 varchar 值“Id”转换为数据类型 int 时转换失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42127992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion failed when converting the varchar value 'Id' to data type int
提问by jhovyn
I got this error when trying run my sql query...
尝试运行我的 sql 查询时出现此错误...
Conversion failed when converting the varchar value 'Id' to data type int
将 varchar 值“Id”转换为数据类型 int 时转换失败
SELECT *
FROM History
INNER JOIN Header
ON History.Id = Header.docId
Please help me :(
请帮我 :(
回答by P?????
In your Joincondition am sure one column is of Integertype and other one is Varchartype.
在您的Join情况下,请确保一列是Integer类型,另一列是Varchar类型。
ON History.Id = Header.docId
since Inthas higher precedence than varchar, Varcharcolumn will be implicitly converted to Int
由于Int优先级高于varchar,Varchar列将被隐式转换为Int
So explicitly convert the Intcolumn to varchar.
因此,将Int列显式转换为 varchar。
ON History.Id = cast(Header.docId as varchar(50))
I have considered Header.docIdas Inttype if no, then convert History.Idto varchar
如果没有,我认为Header.docId是Int类型,然后转换History.Id为varchar
回答by Shakeer Mirza
Try casting Id column to INT
尝试将 Id 列转换为 INT
SELECT *
FROM History
INNER JOIN Header
ON cast(History.Id AS int) = Header.docId

