gpt4 book ai didi

sql-server - SQL Server:将表达式转换为数据类型bigint的算术溢出错误

转载 作者:行者123 更新时间:2023-12-02 06:35:23 24 4
gpt4 key购买 nike

这是我的查询顺序:

SELECT
CASE
WHEN ((BarCode IS NOT NULL) AND (ExternelBarCode IS NULL)) THEN BarCode
WHEN ((BarCode IS NULL) AND (ExternelBarCode IS NOT NULL)) THEN CAST(ExternelBarCode AS bigint)
ELSE ExternelBarCode -- When both exist
END AS TicketBarCode
...

运行它时,我收到以下消息:

Arithmetic overflow error converting expression to data type bigint.



BarCode的类型为 bigint,列 ExternelBarCode的类型为 varchar(250)
BarCode = 178625624324,
ExternelBarCode = 2015591149641227565492

我该如何解决这个问题?

最佳答案

2,015,591,149,641,227,565,492的值大于bigint 2 ^ 63-1(9,223,372,036,854,775,807)中允许的最大值,因此无法进行转换。

如果需要返回数字值,可以将其强制转换为decimal(38,0)

SELECT
CASE
WHEN ((BarCode IS NOT NULL) AND (ExternelBarCode IS NULL)) THEN CAST(BarCode as decimal(38,0))
WHEN ((BarCode IS NULL) AND (ExternelBarCode IS NOT NULL)) THEN CAST(ExternelBarCode AS decimal(38,0))
ELSE cast(ExternelBarCode as decimal(38,0)) -- When both exist
END AS TicketBarCode

关于sql-server - SQL Server:将表达式转换为数据类型bigint的算术溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20674058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com