Someone asked why I would use TRY_PARSE after I posted a question at SQL Server Central: Getting the Average. Isn’t is slower?
A fair question. This quick post looks at why.
Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.
A Quick Setup
The question above has the setup code, but what if I add another row? For example, I’ll run this .
insert dbo.commission
(
salesperson
, commission
)
values
(‘Steve’, ‘A’)
Now, let’s look at the data and run the query from the question.
This works. However, let’s remove the (slow) TRY_PARSE() from the aggregate.
Error. Why? I can’t convert “A” in the AVG to a number. It fails.
You might think, I’ll never get bad data like this. But you might? A user might enter something you don’t expect. An AI might model this as a string, which is bad, but it happens. If it’s an EAV type table, or there are other data items and you’re trying to extract the numbers from here, TRY_PARSE is helpful.
SQLNewBlogger
I wrote a post last week and this is a followup that really just took less than 5 minutes to setup and run. Plus I responded for the user in the post.
This showcases me thinking about a question and situation and really gives an interviewer something to ask me. This lets them dive into my thought process and gives them confidence I don’t just write code without thinking.
Add to your blog with short posts like this (or drop on LinkedIn).
