title: Variable for date based on last table update - SQL Server
body: <p>I'm trying to create a date variable based on the last run date of a table, "b." That is, a 4 month lag from the date fetched by this query:</p>
  
  <pre><code>select top 1 rundate from b
  </code></pre>
  
  <p>It produces the date '202003.'</p>
  
  <p>This is my code that is attempting to produce the variable date:</p>
  
  <pre><code>declare @four_month_lag_from_table_update varchar (6)
  
  set @four_month_lag_from_table_update = CAST( year((select top 1 rundate from b)) as varchar(4)) + 
  right('00' + (select top 1 rundate from b)-4, 2)
  
  select @four_month_lag_from_table_update
  </code></pre>
  
  <p>I get the error code 
  "Conversion failed when converting date and/or time from character string."</p>
  
  <p>Expected result is 4 months prior to last run date, so if last run date is 202003 expected result is 201911.</p>
  
  <p>Please tell me what I'm doing wrong?</p>