title: SQL add to WHERE clause based on condition
body: <p>How can I add a condition inside the WHERE CLAUSE based on a case or if condition?</p>
  
  <pre><code>SELECT C.CallID, A.GroupName,A.Assignee, C.CallStatus, C.RecvdDate, C.Urgency, C.Category, C.CallType, C.KPIreport, C.CallDesc,
  DATEDIFF(DAY, C.RecvdDate,GETDATE()) [DurationInDays],
  CASE 
      WHEN DATEDIFF(DAY, C.RecvdDate,GETDATE()) &lt; 30 
          THEN 'Less than 30'
      WHEN DATEDIFF(DAY, C.RecvdDate,GETDATE()) &gt;= 30 AND DATEDIFF(DAY, C.RecvdDate,GETDATE()) &lt; 60  
          THEN '30-59'
      WHEN DATEDIFF(DAY, C.RecvdDate,GETDATE()) &gt;= 60 AND DATEDIFF(DAY, C.RecvdDate,GETDATE()) &lt; 90  
          THEN '60-89'
      WHEN DATEDIFF(DAY, C.RecvdDate,GETDATE()) &gt;= 90 AND DATEDIFF(DAY, C.RecvdDate,GETDATE()) &lt; 120  
          THEN '90-119'
      WHEN DATEDIFF(DAY, C.RecvdDate,GETDATE()) &gt;= 120 
          THEN 'Over 120'
  END 
  
  AS 'AgeClassification'
  
  FROM
      CallLog C
      INNER JOIN Asgnmnt A ON C.CallID = A.CallID
  WHERE
      A.HEATSeq =(SELECT MAX(HEATSeq)FROM Asgnmnt WHERE (CallID = C.CallID))
      AND UPPER(A.GroupName) = @GroupName
      AND LOWER(A.EMail) IN (@Assignee)
      AND UPPER(C.CallStatus) = @RecordType
  
      AND 
      CASE WHEN UPPER(C.CallStatus) = 'CLOSED' THEN
              C.ClosedDate &gt;= @StartDate AND C.ClosedDate &lt;= @EndDate
           WHEN UPPER(C.CallStatus) = 'REQUEST TO CLOSE' THEN
              A.DateResolv &gt;= @StartDate AND A.DateResolv &lt;=@EndDate
      END
  
  
  
  
  ORDER BY A.GroupName, A.Assignee,  C.RecvdDate, C.CallID
  </code></pre>
  
  <p>My logic here is that, when the @RecordType is 'CLOSED', the <code>WHERE</code> clause must validate the closed date and if the @RecordType is 'REQUEST TO CLOSE' then it must validate the resolved date.</p>
  
  <p>I also tried doing this using IF statement but I get the same error.</p>