2017-03-09 83 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,