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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:07:12  来源:igfitidea点击:

Conversion failed when converting the varchar value 'Id' to data type int

sqlsql-servertype-conversion

提问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优先级高于varcharVarchar列将被隐式转换为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.docIdInt类型,然后转换History.Idvarchar

回答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