title: SQL Server : conversion failed when converting date and/or time from character string
body: <p>I am creating a procedure in Microsoft SQL Server 2012 but I have a problem. The error message is:</p>
  
  <blockquote>
    <p>Msg 241, Level 16, State 1, Line 4<br>
    Conversion failed when converting date and/or time from character string.</p>
  </blockquote>
  
  <p>Here is my procedure:</p>
  
  <pre><code>DECLARE @name varchar(255)
  
  SELECT name 
  FROM sysobjects 
  WHERE type = 'P' 
    AND (name LIKE 'pr_My_app_log_%' OR name LIKE 'pr_My_item_log_%') 
    AND (DATEDIFF(dd, getdate(), RIGHT(name, 8)) &lt; -2)
  </code></pre>
  
  <p>There are procedures with these names in my database: </p>
  
  <pre><code>pr_My_app_log_20160830
  pr_My_app_log_20160829
  </code></pre>
  
  <p>It should be something wrong with DATEDIFF. Can someone help me to correct the query and explain a little bit the issue?</p>
  
  <p>Thank you.</p>