title: SQL Query to Get the Nearest Date
body: <p>I have a really complex query. I have two tables as below.</p>
  
  <p>Table 1 </p>
  
  <pre><code>EmployeeId Status DateA
  
     A        BIL   1/1
     A        BON   2/1
     A        BIL   2/1 
     A        BIL   3/1 
     A        BIL   4/1 
     A        BIL   5/1
     A        BIL   6/1
  </code></pre>
  
  <p><strong>Table 2:</strong></p>
  
  <pre><code>Employee ID  Reason ExDate   DateB
  
     A          IN   1/1     
     A          OUT  4/15       **4/1**
     A          IN   6/1
     A          OUT  8/15       **9/1**
  </code></pre>
  
  <p>So , the problem is. I want to update the DateB Column in Table 2 with the Value from Date A from Table 1. The condition for that are</p>
  
  <ol>
  <li>Reason column in Table 1 = BIL</li>
  <li>I want to pick that date from Date A column which is the closest ( 30 Days interval) to ExDate with Reason column value = 'OUT'</li>
  </ol>
  
  <p>I am primarily stuck with the 2nd Statement.</p>
  
  <p>Any help is appreciated.</p>