title: Nested If Statement Using Case Statement in SQL Server
body: <p>I inherited a spreadsheet from a co-worker who used a nested If statement as shown below:</p>
  
  <pre><code>  =IF(AND([@[Pred Type]]="PR_FS",[@[ST Finish]]="A"),
        0,
        IF(AND([@[Pred Type]]="PR_SS", OR([@[ACT_START_DATE]]&lt;&gt;0,[@[ST Start]]="A")),
           0,
           IF(AND([@[Pred Type]]="PR_SF",OR([@[ACT_START_DATE]]&lt;&gt;0,[@[ST Start]]="A")),
              0,
              IF(AND([@[Pred Type]]="PR_FF",[@[STFinish]]="A"),0,1)
             )
          )
       )
  </code></pre>
  
  <p>I know I can write a CASE statement that would mimic this but can't figure out the syntax for it. I tried:</p>
  
  <pre><code>,CASE   WHEN #Preds.Pred_Type = 'PR_FS' and #Tbl_Sched_Act.Expected_Finish = 'A' THEN 0 
      WHEN #Preds.Pred_Type = 'PR_SS' OR ACT_START_DATE IS NOT NULL OR Start_RAW IS NOT NULL THEN 0
      WHEN #Preds.Pred_Type = 'PR_SF'OR ACT_START_DATE IS NOT NULL OR Start_RAW IS NOT NULL THEN 0
      WHEN #Preds.Pred_Type = 'PR_FF' and #Tbl_Sched_Act.Expected_Finish = 'A' THEN 0 
      ELSE 1
      END as PRED_MET
  </code></pre>
  
  <p>However, the CASE statement does not evaluate any of the items to be equal to 1. They all evaluate to 0 which isn't correct.</p>
  
  <p>Does anyone have any idea on how I could write my CASE statement correctly?</p>