2013-12-19 52 views
1

這裏是我的查詢的序列:SQL服務器:算術溢出錯誤將表達式轉換爲數據類型BIGINT

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 
... 

當我運行它,我有以下消息:

算術溢出將表達式轉換爲數據類型bigint時出錯。

BarCodebigint類型,並且列ExternelBarCodevarchar(250)類型。

BarCode = 178625624324, 
ExternelBarCode = 2015591149641227565492 

我該如何解決這個問題?

+1

嘗試浮動/十進制(6,2)和其他投一部分,也是 – KumarHarsh

+0

哪些數據是試圖從'轉換ExternelBarCode'? – Szymon

+0

儘量使用@Szymon所說的 –

回答

6

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 
相關問題