title: How do I select previous date from my temp #date table in sql?
body: <p>Hi have one <code>temp #Dates table</code> which is populated by using following condition.</p>
  
  <pre><code>IF @Period = 'Monthly'
      BEGIN
          INSERT #Dates
          SELECT MAX(DATE) WeekDays FROM TABLENAV  WITH (NOLOCK, READUNCOMMITTED) 
          GROUP BY DATEDIFF(MONTH, 0, DATE)
          ORDER BY MAX(DATE) DESC
      END
  </code></pre>
  
  <p>now data is in #Dates temp table is </p>
  
  <pre><code>DATE
  2013-04-22 00:00:00.000
  2013-03-29 00:00:00.000
  2013-02-28 00:00:00.000
  2013-01-31 00:00:00.000
  2012-12-31 00:00:00.000
  2012-11-30 00:00:00.000
  </code></pre>
  
  <p>I want to get the <code>Previous_Date</code> when I'm passing any <code>Todays_Date</code></p>
  
  <p>I tried like following</p>
  
  <pre><code>DECLARE @Todays_Date datetime = '2013-04-22'
      select date from #Dates where MONTH(DATE) = MONTH(@Todays_Date)-1  AND YEAR(DATE) = YEAR(@Todays_Date)
  </code></pre>
  
  <p><code>giving ans : 2013-03-29</code></p>
  
  <p>But when Year changes , I'm getting nothing as ans</p>
  
  <p>For example
  If <code>@Todays_Date = '2012-01-31'</code> it should return me <code>2012-12-31</code> but giving nothing. </p>
  
  <p>Help me to modify my <code>select statement</code>.</p>