title: SQL query not returning all rows
body: <p>I have a table in SQL Server which has many rows, with a <code>created_date</code> column. This column has rows starting from the year 2006.</p>
  
  <p>I want to get all the rows which were created in and before February, 2015. This stored procedure has a parameter <code>@month</code>. It should select all the rows based on the <code>@month</code> value entered.</p>
  
  <p>Here is my query:</p>
  
  <pre><code>select * 
  from products 
  where 1=1 
    and year(created_date) &lt;= 2015 
    and month(created_date) &lt;= @month 
  </code></pre>
  
  <p>But this query returns only the records which were created in and before <strong>February</strong> month of previous years <strong>excluding</strong> records which were created in other months of 2014 (e.g., 2014-03-17, 2014-05-05 are excluded).</p>
  
  <p>I have to get a new date based on the @month entered. Suppose I entered month July, I want to have condition "where created_date &lt; 2015-07-31". So I can do something like this, </p>
  
  <p>So I have changed my query,</p>
  
  <pre><code>declare @date datetime
  set @date = CAST((2015 + '-' + @month + '-' + 28) as datetime)
  select * 
  from products 
  where 1=1 
  and year(created_date) &lt;= 2015 
  </code></pre>
  
  <p>But this query returns 1905-08-08 00:00:00.000 and I want to get 2015-02-28 00:00:00.000 and also I have to find total number of days based on the @month entered so that I can pass that number to CAST((2015 + '-' + @month + '-' + <strong>28</strong>) as datetime) instead of 28.</p>