2017-07-21 36 views
0

我有一個查詢,我做了多個連接來獲取我的一些字段的文本值。問題是我不斷收到一個錯誤,指出查詢給出了一個錯誤,說有一個問題,將一個值轉換爲整數

將varchar值「1774.14」轉換爲數據 類型爲int時轉換失敗。

下面是我的查詢......

SELECT t1.changedon as [Changed On], t1.fldname as [Field Name], 
CASE WHEN (t1.Fldname='LastKnownProvider' and t1.oldValue > 1000) THEN coalesce((t4.ProviderName + ' (' + t4.ProviderAddress + ', ' + t4.ProviderCity + ')'),cast(t1.oldvalue as varchar)) ELSE coalesce(t2.label,cast(t1.oldvalue as varchar)) END as [Old Value], 
CASE WHEN (t1.Fldname='LastKnownProvider' and t1.newValue > 1000) THEN coalesce((t5.ProviderName + ' (' + t5.ProviderAddress + ', ' + t5.ProviderCity + ')'),cast(t1.Newvalue as varchar)) ELSE coalesce(t3.label, cast(t1.NewValue as varchar)) END as [New Value] 
CASE WHEN t1.Changedfrom ='Upload' THEN 'UPLOAD' ELSE 'manual' END AS [Upload/Manual] 
FROM tblFieldChanges t1 
LEFT JOIN tblReferenceData t2 on t1.oldvalue=cast(t2.id as varchar) 
LEFT JOIN tblReferenceData t3 on t1.newValue=cast(t3.id as varchar) 
left join tblProviders t4 on t1.oldvalue = cast(t4.ProviderID as varchar) 
left join tblProviders t5 on t1.NewValue = cast(t5.ProviderID as varchar) 
WHERE TblName = 'tblClients' AND PKID = 8558 

我tblFieldChanges表看起來像這樣...

enter image description here 此表包含在更新已發生的磁場變化,所以數據樣本數據可能看起來像這樣...

ID tblName FldName PKID Oldvalue NewValue ChangedOn 
17 tblClients Cost  19  1774.14 1775.19 7/21/2017 8:45am  

其他表名稱,主要是tblReferenceData和tblProviders都有一個ID和我抓取的文本值(特別是tblReferenceData)。任何人都知道我爲什麼會出現錯誤。它似乎不僅在錯誤情況下出錯,而且如果我有諸如城市(舊金山)之類的文本值,則會得到類似的錯誤,指出在轉換varchar時轉換失敗值'舊金山'到數據類型int。

任何幫助,非常感謝。

+0

創建一個測試用例,即刪除所有內容,直到擁有最小版本。當你在這時,你可能也應該用文字替換圖片。 –

+0

糟糕的桌子設計......重新設計,問題就會消失。 – jarlh

+0

錯誤似乎很明顯。 '1774.14'不是一個有效的整數,但是你將它與一個整數(1000)進行比較。 SQL正在進行隱式轉換。將列類型更改爲正確(例如Decimal(19,2))或將varchar oldvalue轉換爲十進制,這將允許您進行比較。我強烈建議固定列類型而不是鑄造,但這是你的葬禮! –

回答

1

由於OP聲明數據類型不能改變。這裏是通過做比較之前鑄造的OldValue和的NewValue數值爲十進制的解決方案:

SELECT t1.changedon as [Changed On], t1.fldname as [Field Name], 
CASE WHEN (t1.Fldname='LastKnownProvider' and CAST(t1.oldValue AS DECIMAL(19,2)) > 1000) THEN coalesce((t4.ProviderName + ' (' + t4.ProviderAddress + ', ' + t4.ProviderCity + ')'),cast(t1.oldvalue as varchar)) ELSE coalesce(t2.label,cast(t1.oldvalue as varchar)) END as [Old Value], 
CASE WHEN (t1.Fldname='LastKnownProvider' and CAST(t1.newValue AS DECIMAL(19,2)) > 1000) THEN coalesce((t5.ProviderName + ' (' + t5.ProviderAddress + ', ' + t5.ProviderCity + ')'),cast(t1.Newvalue as varchar)) ELSE coalesce(t3.label, cast(t1.NewValue as varchar)) END as [New Value] 
CASE WHEN t1.Changedfrom ='Upload' THEN 'UPLOAD' ELSE 'manual' END AS [Upload/Manual] 
FROM tblFieldChanges t1 
LEFT JOIN tblReferenceData t2 on t1.oldvalue=cast(t2.id as varchar) 
LEFT JOIN tblReferenceData t3 on t1.newValue=cast(t3.id as varchar) 
left join tblProviders t4 on t1.oldvalue = cast(t4.ProviderID as varchar) 
left join tblProviders t5 on t1.NewValue = cast(t5.ProviderID as varchar) 
WHERE TblName = 'tblClients' AND PKID = 8558 

這也可以通過使用字符串比較來完成,但是這是一個非常不好的做法:

t1.oldValue > '1000'

相關問題