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 Join
condition am sure one column is of Integer
type and other one is Varchar
type.
在您的Join
情况下,请确保一列是Integer
类型,另一列是Varchar
类型。
ON History.Id = Header.docId
since Int
has higher precedence than varchar
, Varchar
column will be implicitly converted to Int
由于Int
优先级高于varchar
,Varchar
列将被隐式转换为Int
So explicitly convert the Int
column to varchar.
因此,将Int
列显式转换为 varchar。
ON History.Id = cast(Header.docId as varchar(50))
I have considered Header.docId
as Int
type if no, then convert History.Id
to 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