title: SQL that outputs the last July 1 that is less than two years away
body: <p>So I'm trying to correct some SQL that's attempting to output the last July 1 that is less than two years away from a certain date. So 06/30/2014 would return 07/01/2015 since 07/01/2016 is more than two years in the future. The date 07/02/2014 should return 07/01/2016, but the SQL below is returning 07/01/2015 incorrectly. </p>
  
  <pre><code>DECLARE @Year VARCHAR (10) 
  DECLARE @Date VARCHAR(10)
  DECLARE @OrigDate VARCHAR (10) = /*substitution for relevant date here*/
  
  SET @Year=(DATEPART (YYYY,DATEADD(YYYY,2,GETDATE()))) 
  SET @Date = (SELECT '07/01/'+@Year)
  
  IF @OrigDate &gt; DATEADD(YEAR,-2,GETDATE()) 
   BEGIN 
      SET @Year=(DATEPART (YYYY,DATEADD(YYYY,1,GETDATE()))) 
      SET @Date = (SELECT '07/01/'+@Year)
   END
  
  SELECT @Date AS RetValue
  </code></pre>
  
  <p>Now that we're in August, it's firing correctly, but I need to fix it before next July rolls around. (The date isn't always the current date, but it's usually close)</p>