title: SQL Server date comparisons based on month and year only
body: <p>I am having trouble determining the best way to compare dates in SQL based on month and year only.</p>
  
  <p>We do calculations based on dates and since billing occurs on a monthly basis the date of the month has caused more hindrance.</p>
  
  <p>For example</p>
  
  <pre><code>DECLARE @date1 DATETIME = CAST('6/15/2014' AS DATETIME),
          @date2 DATETIME = CAST('6/14/2014' AS DATETIME)
  
  SELECT * FROM tableName WHERE @date1 &lt;= @date2
  </code></pre>
  
  <p>The above example would not return any rows since @date1 is greater than @date2. So I would like to find a way to take the day out of the equation.</p>
  
  <p>Similarly, the following situation gives me grief for same reason.</p>
  
  <pre><code>DECLARE @date1 DATETIME = CAST('6/14/2014' AS DATETIME),
          @date2 DATETIME = CAST('6/15/2014' AS DATETIME),
          @date3 DATETIME = CAST('7/1/2014' AS DATETIME)
  
  SELECT * FROM tableName WHERE @date2 BETWEEN @date1 AND @date3
  </code></pre>
  
  <p>I've done inline conversions of the dates to derive the first day and last day of the month for the date specified.</p>
  
  <pre><code>SELECT * 
  FROM tableName 
  WHERE date2 BETWEEN
      DATEADD(month, DATEDIFF(month, 0, date1), 0) -- The first day of the month for date1
      AND
      DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, date2) + 1, 0)) -- The lastday of the month for date3
  </code></pre>
  
  <p>There has to be an easier way to do this. Any suggestions?</p>