title: How to get the nearest date to a record date in SQL Server?
body: <p>I have the following data structure:</p>
  
  <p><strong>Table A</strong>:</p>
  
  <pre><code>ID  |  RequestNumber  |  Date
  ----+-----------------+-----------
   1  |      1          | 2017/09/27
   2  |      1          | 2018/06/02
  </code></pre>
  
  <p><strong>Table B</strong>:</p>
  
  <pre><code>RequestNumber  |  Serial  |  Date
  ---------------+----------+-----------
       1         |    1     | 2017/09/27
       1         |    2     | 2017/09/27
       1         |    6     | 2018/06/03
       1         |    7     | 2018/06/03
       1         |    8     | 2018/06/03
  </code></pre>
  
  <p>As we can see the nearest date to the first row of <code>Table A</code> is 2017/09/27  in the <code>Table B</code> and the nearest date to the second row is 2018/06/03 in the <code>Table B</code></p>
  
  <p>So...</p>
  
  <p>I need a query to have each row from <code>Table A</code> with all rows from <code>Table B</code> that is the nearest to the record from <code>Table A</code> (it means 2 records should be returned the first record and 3 records should be returned for the second record)</p>
  
  <p>The expected result would be:</p>
  
  <pre><code>ID  |  RequestNumber  |  Serial  |  Date
  ----+-----------------+----------+------------
   1  |      1          |    1     | 2017/09/27
   1  |      1          |    2     | 2017/09/27
   2  |      1          |    6     | 2018/06/03
   2  |      1          |    7     | 2018/06/03
   2  |      1          |    8     | 2018/06/03
  </code></pre>
  
  <p>Thanks in advance</p>