title: SQL query to select equal or less than or greater than
body: <p>Assuming that there is a table t with the following columns: Code int, Name nvarchar(50).</p>
  
  <p>I'd would like to query the table for the most matching row for a given Code c.  The 'most matching' criteria (in order of importance):</p>
  
  <ul>
  <li>1) select a row whose Code matches c </li>
  <li>2) select a row whose Code is greater than c (but the very first one).  For example, if c = 4 and t
  contains 1, 2, 3, 5, 6, and 7, I'd like to select 5. </li>
  <li>3) select a row whose
  Code is less than c.  For example, if c = 4 and t contains 3, 2, and
  1, I'd like to select 3.</li>
  </ul>
  
  <p>The code is going to be in a stored procedure.</p>
  
  <p>Could someone please suggest how to accomplish the above.</p>
  
  <p>Thanks.</p>
  
  <p>Sample data and expected results:</p>
  
  <pre><code>1, "Name1"
  2, "Name2"
  4, "Name4"
  5, "Name5"
  
  If c=2, result: 2,"Name2"
  If c=3, result: 4,"Name4"
  if c=6, result: 5,"Name5"
  </code></pre>