2013-06-03 69 views
-1

我有一個名爲runTotal的字段名,它是一個整數。如何解決CASE語句錯誤?

我試圖將值賦給最高的總CASE語句:

select (case when isnull(t.runtotal,90) = 0 then 'You have reached max total' else t.runtotal end) runtotal

我收到以下錯誤:

Conversion failed when converting the varchar value 'Class is full' to data type int 

如果我刪除否則t.runtotal,它的工作原理,但我們希望的是別人的代碼,因爲默認值應該是90

任何想法如何解決這個問題?

非常感謝提前

+1

您可以通過鑄造'runtotal'爲varchar值'投解決這個問題(t.runtotal爲varchar(10))'那麼數據類型將是相同的。 – Taryn

回答

0

我終於解決了這個內容問題。下面是最終的解決方案:

(case when isnull(t.runtotal,9) = 0 then 'You have reached max total' 
else str(isnull(t.runtotal,9)) end) runtotal 
3

這是因爲你正在返回不同的數據類型。
第一部分返回一個varchar,而else部分返回一個數字。

select 
-- this condition returns a varchar 
(case when isnull(t.runtotal,90) = 0 then 'You have reached max total' 
-- else condition is returning a number 
else t.runtotal 
end) runtotal. 

返回值的數據類型應該是相同的。
請糾正它。

0

此代碼是說

select (case when isnull(t.runtotal,90) = 0 
    then 'You have reached max total' 
    else t.runtotal end) runtotal 

如果runTotal = 0,回到我一個字符串消息文本「您已達到最大總」。 否則,返回我一個整數告訴我跑總

這是說,還給我提供一個錯誤信息字符串,或運行總計轉換爲字符串

select (case when isnull(t.runtotal,90) = 0 
    then 'You have reached max total' 
    else Str(t.runtotal) end) runtotal 
+0

所有回覆的人,我非常感謝你。 @shahkalpesh,我沒有看到你的解決方案和我的任何區別。事實上,當我跑你的時候,我發現與我發佈的錯誤一樣。 Sparky,沒有與您的錯誤,但再次,而不是runtotal爲null時,默認值爲90,所有行都是空白的。 任何其他的想法如何解決這個問題? –

+0

當runTotal爲NULL時,你想顯示什麼?如果你想顯示它,你可以簡單地做IsNull(t.runtotal,90)作爲RunTotal,但是你要在你的代碼中進行比較......添加一些樣本預期輸出,我想人們將能夠爲你闡明SQL ... – Sparky

+0

是的,當runTotal爲null且isNull(t.runtotal,90)爲runTotal時,我可以顯示90,但我們希望能夠在值爲0時顯示'您已經達到最大總數'。這就是關鍵。我不確定誰扣2分。當你的問題不清楚時,我認爲他們這樣做。這個問題不可能更清楚。 當我們第一次運行應用時,我們想要將值加載爲90,並且我們將繼續向下計數直到0.我在這個論壇上看到類似的東西,但它對我不起作用。 –