消息 8114,级别 16,状态 5,第 31 行将数据类型 varchar 转换为 bigint 时出错

Msg 8114, Level 16, State 5, Line 31 Error converting data type varchar to bigint

我正在尝试将 varchar 列转换为 bigint,但一直出现错误。

我已尝试使用每种精确或近似的数据类型来更改列,但它无法转换。我已经整理并清理了数据,但它仍然存在问题。

这是我一直在使用的代码。

USE [database1]
GO

ALTER TABLE [dbo].[table1]
ALTER COLUMN [column1] bigint

GO

这些是上面代码的结果

Msg 8114, Level 16, State 5, Line 31
Error converting data type varchar to bigint.
The statement has been terminated.

感谢任何帮助,谢谢。

您的值似乎无效。因此,将它们更新掉:

update t
    column1 = convert(varchar(255), try_convert(bigint, column1));

然后尝试更改列类型。

或者,只需添加一个计算列:

alter table t add column1_bi as (try_convert(bigint, column1));

您可以看到使用了哪些错误值:

select column1
from t
where try_convert(bigint, column1) is null and column1 is not null;