在 Sql server 2008 中将 nvarchar 转换为 bigint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5828510/
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 10:21:07  来源:igfitidea点击:

Convert nvarchar to bigint in Sql server 2008

sqlsql-server-2008nvarcharbigint

提问by Hossein Moradinia

I want insert all rows of a table into another table, and I also want convert a nvarcharfield into bigint, but when I use convert(bigint, col1)SQL Server shows an error:

我想将一个表的所有行插入到另一个表中,我也想将一个nvarchar字段转换为bigint,但是当我使用convert(bigint, col1)SQL Server 时显示错误:

Error converting data type nvarchar to bigint

将数据类型 nvarchar 转换为 bigint 时出错

How can I fix this problem?

我该如何解决这个问题?

回答by marc_s

You could try to use ISNUMERICto determine those rows that are indeed numeric:

您可以尝试使用ISNUMERIC来确定那些确实是数字的行:

UPDATE dbo.YourTable
SET BigIntColumn = CAST(NVarcharColumn AS BIGINT)
WHERE ISNUMERIC(NVarcharColumn) = 1

That would convert those rows that can be converted - the others need to be dealt with manually.

这将转换那些可以转换的行 - 其他的需要手动处理。

回答by Gamal Weshahy

You should convert bigint to nvarchar not vice versa cast(Other_Column_name as nvarchar) not cast (Column_Name as bigint)

您应该将 bigint 转换为 nvarchar,反之亦然 cast(Other_Column_name as nvarchar) not cast (Column_Name as bigint)