title: ASP/SQL Server - from/to dates based on months of year
body: <p>I have an ASP page linked to SQL server. Basically I need some queries will extract all records between certain dates. I need a monthly breakdown for the last 6 months.</p>
  
  <p>So, this month is August (it's the 6th), so the first query needs to have a line that says</p>
  
  <pre><code>AND DATE BETWEEN '2012-08-01' and '2012-08-31'
  </code></pre>
  
  <p>And I need 5 more queries which say</p>
  
  <pre><code>AND DATE BETWEEN '2012-07-01' and '2012-07-31'
  </code></pre>
  
  <p>...</p>
  
  <pre><code>AND DATE BETWEEN '2012-06-01' and '2012-06-30'
  </code></pre>
  
  <p>Etc. etc going back to March.</p>
  
  <p>Is there a way to do this? I've tried to do the following VB script but it's not giving the correct result;</p>
  
  <pre><code>ThisMonth = Date
  Month1 = DateAdd("m", -1, Date)
  Month1From = DateAdd("d", 1, Month1 - Day(Date))
  Month1To = DateAdd("m", 1, Month1 - Day(Date))
  
  Month2 = DateAdd("m", -2, Date)
  Month2From = DateAdd("d", 1, Month2 - Day(Date))
  Month2To = DateAdd("m", 1, Month2 - Day(Date))
  
  Month3 = DateAdd("m", -3, Date)
  Month3From = DateAdd("d", 1, Month3 - Day(Date))
  Month3To = DateAdd("m", 1, Month3 - Day(Date))
  
  Month4 = DateAdd("m", -4, Date)
  Month4From = DateAdd("d", 1, Month4 - Day(Date))
  Month4To = DateAdd("m", 1, Month4 - Day(Date))
  
  Month5 = DateAdd("m", -5, Date)
  Month5From = DateAdd("d", 1, Month5 - Day(Date))
  Month5To = DateAdd("m", 1, Month5 - Day(Date))
  </code></pre>
  
  <p>I was going to construct the SQL based on the variables, but I cannot, as the variable Month5To is showing as the 29th of Match, but there are 31 days in March. </p>
  
  <p>Can anyone help?</p>