在SQL Server中更改if语句的时间

时间:2021-10-13 10:39:29

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.

如何将以下代码更改为返回布尔值0或1值的if语句?我希望得到的最终结果是一列列出利率为2,如果条件为真,我的结果列为0或1。

(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,

Currently, I am getting something like below:

目前,我得到的内容如下:

Results Table

结果表

2 个解决方案

#1


1  

To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.

要过滤行,请使用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

#2


0  

Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone

找到我的答案,就像添加“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,

#1


1  

To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.

要过滤行,请使用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

#2


0  

Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone

找到我的答案,就像添加“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,