title: SQL Query with Optional Parameter and Possible Null Column
body: <p>I am having some difficulty returning all the results I would expect when leaving an optional sql parameter blank on a column that contain null values.</p>
  
  <p>Imagine you have a table with the following (referredby is optional and can therefore be NULL):  </p>
  
  <pre><code>Customertable
  ID    CustomerName  ReferredBy
  1      Aaron         Joe
  2      Peter         NULL
  3      Steven        Joe
  </code></pre>
  
  <p>Suppose I want to query with an optional SQL parameter for the referredby field like such:</p>
  
  <pre><code>declare @referredby as varchar(15)
  
  select id, customername
  from customertable&lt;br&gt;
  where referredby = isnull(@referredby, referredby)
  </code></pre>
  
  <p>If I leave the parameter null, this would only return:<br>
  1 Aaron<br>
  3 Steven</p>
  
  <p>How can I use an optional parameter to return all 3 results?</p>