You're getting the error because of the CAST
statement inside the try_convert
- try_convert
won't consume all the errors you generate, it will just return NULL
if your convert fails.
If you use try_convert
for both attempts it will work:
when try_convert(datetimeoffset, (try_convert(datetimeoffset, substring(@DateString,1,10) + ' ' + substring(@DateString,12,8) + ' ' + substring(@DateString,20,6)))) IS NULL