2017-03-09 82 views
0

如何將以下代碼更改爲返回布爾0或1值的if語句?我希望得到的最終結果是列出2的利率的一列,如果條件爲真,則結果列的值爲0或1。更改SQL服務器中的if語句的時間

(Case when new_interestratevariability = 2 
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6) 
    and new_crms_dt = @Curr_Date 
    then 0 else 1 end) as CIEDIT_VAL_96, 

目前,我得到的東西象下面這樣:

Results Table

+1

那究竟是什麼問題呢? – Mureinik

+0

你不知道。 IF語句用於控制處理流程。案例表達式用於確定單個列中要返回的值。 –

+0

我的結果列顯示的是不等於2的VariableA值,只想顯示等於2的VariableA值。如果這很有意義 –

回答

1

要過濾行,使用WHERE子句。 Select子句中的Case語句將修改行中顯示的值。

Select * 
from table 
Where new_interestratevariability = 2 
     and new_interestrateindex IN (1,2,3,4,6) 
     and new_crms_dt = @Curr_Date 
+0

雖然此代碼段可以解決這個問題,[包括解釋](http:///meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – DimaSan

+1

@DimaSan,謝謝,這是很好的建議。我的回答更新了,儘管看起來OP在問一個不同的問題。 – Degan

0

找到我的答案,就像添加「not in」而不是「in」一樣簡單。謝謝大家

(Case when new_interestratevariability = 2 
and (new_interestrateindex not in(1,2,3,4,6)) 
    and new_crms_dt = @Curr_Date 
     then 1 else 0 end) as CIEDIT_VAL_96,