title: SQL How to correctly set a date variable value and use it?
body: <p>I have the following query which uses a date variable, which is generated inside the stored procedure:</p>
  <pre><code>DECLARE @sp_Date DATETIME
  SET @sp_Date = DateAdd(m, -6, GETDATE())
  
  SELECT DISTINCT pat.PublicationID
       FROM PubAdvTransData AS pat 
       INNER JOIN PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID
       WHERE (pat.LastAdDate &gt; @sp_Date) AND (pa.AdvertiserID = 12345))
  </code></pre>
  <p>The problem is that the @sp_Date value appears to be being ignored and I am wondering why?  Have I defined or used it incorrectly?</p>