title: Find invalid dates in SQL Server 2008
body: <p>I have a 300.000 rows table; one of the columns is a varchar() but it really contains a date xx/xx/xxxx or x/x/xxxx or similar. But executing the following test yields an error:</p>
  
  <pre><code>SELECT CAST(MyDate as Datetime) FROM MyTable
  </code></pre>
  
  <p>The problem is that it doesn’t tell me in which row…</p>
  
  <p>I have executed a series of “manual” updates by trial an error and performed simple updates to fix those, but there’s got to be some weird values that need to either be deleted or fixed.</p>
  
  <p>For example I performed a simple test that fixed about 40 rows:</p>
  
  <pre><code>UPDATE MyTable SET MyDate = REPLACE(MyDate, '/000','/200') FROM MyTable WHERE MyDate like ('%/000%’)
  UPDATE MyTable SET MyDate = REPLACE(MyDate, '/190','/199') FROM MyTable WHERE MyDate like ('%/190%’)
  </code></pre>
  
  <p>This fixed quite a few weird rows that had dates like 01/01/0003 and such. (Dates range from 1998 to 2010). </p>
  
  <p>However, I’d like to know <strong>which</strong> rows are failing in the above select. </p>
  
  <p>What would be the best way to print those so I can either delete them, edit them or see what to do? Thanks.</p>