title: SQL Need two WHERE clause conditions
body: <p>I need a conditional <code>Where</code> clause or <code>Case</code> statement based on a table field's value.</p>
  
  <p>This is what I have and it's not working: </p>
  
  <pre><code>Select n.id, n.message, n.Type
  From Notifications n
  Where
  (
        n.Type = 1 
        And ApplicationMask &amp; isnull(@applicationMask,2147483647)&lt;&gt; 0
        And Cast(GETDATE() as Date) Between Cast(StartDate AS Date) and
                                               Cast(EndDate as Date) 
        And Not Exists(Select notificationID 
                       From NotificationDelivery nd 
                       Where nd.NotificationID = n.id 
                       and nd.UserID = @userID)
  
   )
   OR
   (
        n.Type = 2 
        And ApplicationMask &amp; ISNULL(@applicationMask,2147483647) &lt;&gt; 0
        And Cast(GETDATE() as Date) Between Cast(StartDate AS Date) and 
                                             Cast(EndDate as Date)      
   )
  </code></pre>
  
  <p>The <code>OR</code> is messing it up, I think.  The top <code>Where</code> clause works by itself, but like this it is returning rows that it should not return. It seems like the <code>NOT EXIST</code> part is being ignored. Is there a better way? I am not too strong in SQL queries.</p>