2012-11-02 95 views
2

當加入這一行到我的查詢:的SQL Server 2005:轉換轉換爲varchar值 '0.00' int數據類型時失敗

convert(varchar(20), convert(varchar(20), 
sum(case when tsr.other like '%aa%' then tsr.block1 else 0 end) + 
sum(case when tsr.other like '%aa%' then tsr.block2 else 0 end) + 
sum(case when tsr.other like '%aa%' then tsr.block3 else 0 end) + 
sum(case when tsr.other like '%aa%' then tsr.block4 else 0 end)) * 450) 

我收到此錯誤信息:

轉換將varchar值「0.00」 int類型在嵌段共轉換成數據

數據時失敗的lumn是幾天 - 例如10.0

任何想法?

我已經修好了,它只是把450改成450.0。

varchars的原因是,這只是多個聯合選擇語句中的1行的1行。

+0

忘記了,回答了我自己的問題。將450更改爲450.0並排除錯誤消息。 – user1794118

+0

看起來好像你試圖用450多個varchar(20)否? – Tr1stan

+0

嘗試像這樣... 然後Cast(tsr.block2 as float)else 0.0 end – bummi

回答

1

10.0不是int - 它是小數。

嘗試

declare @i int 
select @i = convert(decimal(9,4),'10.0') 
select @i 

,並從十進制爲int的轉換將隱式進行。

1
'10.0' isn't an int/decimal - it's a varchar . 
do any mathematical calculation only on decimal/numeric/float/int values. 

SELECT convert(varchar(20), convert(decimal(10,2), sum(case when 
tsr.other like '%aa%' then convert(decimal(10,2),tsr.block1) else 0 
end) + sum(case when tsr.other like '%aa%' then 
convert(decimal(10,2),tsr.block2) else 0 end) + sum(case when 
tsr.other like '%aa%' then convert(decimal(10,2),tsr.block3) else 0 
end) + sum(case when tsr.other like '%aa%' then 
convert(decimal(10,2),tsr.block4) else 0 end)) * 450) 
相關問題