title: SQL Len(replace)
body: <p>In SQL Server 2005, I have a <code>timestamp</code> type column. I want to convert to the <code>datetime</code> type. For example:</p>
  
  <pre><code>'20170824112344'
  </code></pre>
  
  <p>But they can come bad. For example:</p>
  
  <pre><code>'____123456____'
  
  _ = blank spaces
  </code></pre>
  
  <p>I want to filter by those with length 14.</p>
  
  <pre><code>case
     when len(replace(fecha,' ','')) = 14 
        then convert(smalldatetime, SUBSTRING(fecha, 7, 2) + '-' + 
                                    SUBSTRING(fecha, 5, 2) + '-' +  
                                    SUBSTRING(fecha, 1,4) + ' ' + 
                                    SUBSTRING(fecha, 9, 2) + ':' + 
                                    SUBSTRING(fecha, 11, 2) + ':00', 103)
        else fecha
  end as fecha
  </code></pre>
  
  <p>but it does not work</p>