title: MSSQL select nearest match (id) of specified condition
body: <p>I have a table filled with some words of different length. </p>
  
  <pre><code>ID | WORD  | LENGTH  
  1  | able  |   4
  2  | acid  |   4
  3  | about |   5
  .....
  </code></pre>
  
  <p>method in C# is generating random number and I want to get word with nearest match of length and ID. Recently I am using this query</p>
  
  <pre><code>select top 1 word from vocabulary where length = 4 and id &lt;= 3;
  </code></pre>
  
  <p>problem is that this way it always returns first occurrence of word with 4 letters. That's not what I need. </p>
  
  <p>I can't use this:</p>
  
  <pre><code>select top 1 word from vocabulary where length = 4 and id &lt;= 3;
  </code></pre>
  
  <p>because when the random number is close to the last id in the table, it could happen that there will be no other word with requested length. (e.g. </p>
  
  <pre><code> select top 1 word from vocabulary where length = 4 and id &gt;= 2; 
  </code></pre>
  
  <p>would not be able to find match. </p>
  
  <p>Is there a way how to select 1 value that has nearest match in the requested direction? &lt;= or >=</p>
  
  <p>Thanks.  </p>