Safe convert from string to date in Transact
<p>I have the following code in a stored procedure in MSSQL Transact:</p>

<pre><code>CAST(CONVERT(Datetime, aof.Transactiondate) AS date)
</code></pre>

<p>aof.Transactiondate is a varchar(8) and should be written in the form of '20160202' or '20160117'.
Today I found out that in rare instances aof.Transactiondate can be a '1' and thus my code crashes completely.</p>

<p>Can I do anything to make sure that the above don't crash and sets a defult date or something instead of crashing??</p>

<p>Theoretically I could make sure that there is 8 chars in the varchar and then seperate the char into 4, 2 and 2 block and finally make sure that block one is between 2015 and 2016 (only relevant at the moment), second block is between 1 and 12 and third block is between 1 and 31 but that seems to be a huge amount of work.</p>