title: using parameterized data bound query, finding number of rows where column is true within last "x" number of days
body: <p>I have a query that returns the number of rows where Column "FA" is 1, between the dates of today and the parameter "x" number of days past.  The query seems to run very slow, and I'm hoping that there might be a better or faster way of making this happen.  The table this is querying has over 3 million rows, so the query gets limited in the application to look thru 259,200 rows at most, where @NewInt is 90 or less.</p>
  
  <p>I have previously ran this query without exception,</p>
  
  <pre><code>SELECT COUNT(*) AS Expr1
  FROM S_Data_Table
  WHERE (FA = 1) AND (t_stamp &gt;= DATEADD(DAY, -90, GETDATE()))
  </code></pre>
  
  <p>Note that the parameter is not present, instead just ran with Int of -90 as days past.</p>
  
  <p>I have found that when I introduce a parameter as the number of days the t_stamp which is data type DateTime needs conversion,</p>
  
  <pre><code>SELECT COUNT(*) AS Expr1
  FROM S_Data_Table
  WHERE (FA = 1) AND (CONVERT(nvarchar, t_stamp, 0) &gt;= DATEADD(DAY, - (@NewInt), GETDATE())) 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/w2TTj.png" rel="nofollow noreferrer">T_stamp pic table adapter</a></p>
  
  <p>This code runs very slowly, I think it is iterating over every row, even the rows outside of the time frame, but I don't know how to find that out?</p>
  
  <p>The normal result of the query when ran produces an Integer value of the number of rows that are true within the time frame.</p>