There's an application that I work with that doesn't properly sanitize input; I'm sure this is an isolated case and none of you have experienced this, but speaking from the hypothetical here is a solution. I stumbled on a query plan last week that was outputting 2 trillion rows when, at the very most, should have had a maximum of 500k. It was causing performance problems (shocking!) and I took the usual optimization steps but found out that including a LTRIM( RTRIM ( ) ) function in a join was one of the causes of the "fat execution plan lines." I removed the function from the join and found that execution time dropped from 12 seconds to less then 1 and the plan was significantly improved. And then found about 50 other sprocs that would be happier if I did the same. Sounded like a good business case to me.
I'll admit I still don't know why this had the effect it did but regardless I knew that having this function in so many joins is a symptom of a common underlying cause: bandaid code written to accommodate bad data. That I can fix.
It's not as easy as LTRIM-RTRIM. Those functions don't strip ASCII characters such as tabs or carriage returns.
I created a trigger on two of our tables with the heaviest use that strips away any unwanted ASCII characters from ONLY the beginning or the end of the string. Doing a find and replace is relatively simple, but only touching leading and trailing characters AND being choosy about which to whitelist? More fun. Here's the scoop below.
The first step is to get a visual of what you're working with. [master].[dbo].[spt_values], my favorite cuddly undocumented table, can help you do this. The first step is to create a Tally table if you don't have one already. I used this code from StackOverflow as a starting point. I kept my rowcount small which isn't typical but I'm only using it on small strings for the time being.