In the previous post we looked at how
TRY_CASTwork and why we may want to utilise the latter when building validation for our data. When SQL Server 2012 rolled around it wasn’t only
TRY_CASTwhich was added, we also had
Here we’re going to look at how those function and the differences in the outputs which they can provide. We’ll start with the sample data below as the basis for these demonstrations:
Andy focuses mostly on the behavioral differences, where I like
TRY_PARSE() a lot, especially because you can control locale for dates and times. When it comes to performance, I’ve found
TRY_CONVERT() to be essentially the same performance-wise. Maybe there’s a tiny difference between the two but there’s no guarantee of it.
TRY_PARSE(), however, calls into the .NET CLI for each execution and will be considerably slower. If you’re parsing a few or a few dozen values, you probably won’t notice the difference. If you’re parsing tens of thousands of values (or more), I assure you that you’ll notice the difference.