title: T-SQL Conditional WHERE Clause
body: <p>Found a couple of similar questions here on this, but couldn't figure out how to apply to my scenario.</p>
  
  <p>My function has a parameter called <strong>@IncludeBelow</strong>. Values are 0 or 1 (BIT).</p>
  
  <p>I have this query:</p>
  
  <pre><code>SELECT p.*
  FROM Locations l
  INNER JOIN Posts p
  on l.LocationId = p.LocationId
  WHERE l.Condition1 = @Value1
  AND   l.SomeOtherCondition = @SomeOtherValue
  </code></pre>
  
  <p>If @IncludeBelow is 0, i need the query to be this:</p>
  
  <pre><code>SELECT p.*
  FROM Locations l
  INNER JOIN Posts p
  on l.LocationId = p.LocationId
  WHERE l.Condition1 = @Value1
  AND   l.SomeOtherCondition = @SomeOtherValue
  AND   p.LocationType = @LocationType -- additional filter to only include level.
  </code></pre>
  
  <p>If @IncludeBelow is 1, that last line needs to be excluded. (i.e don't apply filter).</p>
  
  <p>I'm guessing it needs to be a <code>CASE</code> statement, but can't figure out the syntax.</p>
  
  <p>Here's what i've tried:</p>
  
  <pre><code>SELECT p.*
  FROM Locations l
  INNER JOIN Posts p
  on l.LocationId = p.LocationId
  WHERE l.Condition1 = @Value1
  AND   l.SomeOtherCondition = @SomeOtherValue
  AND (CASE @IncludeBelow WHEN 0 THEN p.LocationTypeId = @LocationType ELSE 1 = 1)
  </code></pre>
  
  <p>Obviously that's not correct.</p>
  
  <p>What's the correct syntax?</p>