title: Conditional WHERE clause
body: <p>I need a solution for a conditional <code>WHERE</code> clause in SQL Server. I can't figure out how to add the functions to the <code>WHERE</code> clause depending on the declared variables. I'll bracket the conditional parts with the needed logic</p>
  
  <pre><code>DECLARE @distance bigint
  DECLARE @photosneeded bigint
  
  SET @distance * 50
  
  SELECT * FROM users u
  WHERE
    [IF @distance &gt; 0 THEN distance_function(lat1,lon1,lat2,lon2)&lt;=@distance END]
    AND
    [IF @photosneeded&gt;0 THEN u.photo IS NOT NULL AND u.photo &lt;&gt;'' END]
  </code></pre>