LTRIM and RTRIM not removing white space


LTRIM and RTRIM not removing white space


I was writing a query today and came across a set of data with an annoying leading space. Or what I thought was a leading space. My first instinct was to throw this around my field:


But the space was still there. OMGWTFBBQ! After a bit of googling, I started to wonder if this was REALLY a space. I decided to try this instead:

REPLACE([SKU], SUBSTRING([SKU], PATINDEX('%[^a-zA-Z0-9 '''''']%', [SKU]), 1), '')

Success! But what was the issue? Apparently, what looked like a leading space was more likely a carriage return, line feed, or some other non-printable ASCII character.

One thing to note: In this case, the need to use patindex or ltrim rtrim around a unique identifier like a SKU is a big red flag that something unexpected is happening during ETL. So while this solution might work in a pinch, if you find yourself in a similar situation, clean up that data!


More About the Author

Tim Rhymer

Solutions Architect
To Infinity and Beyond: The Power of the Cloud in IT As we hit refresh on a new month, new year and new decade, it makes sense for us to look back on the road that brought us to where we ...
How to Use Kaseya to Detect Meltdown and Spectre Vulnerable Machines To help detect client Windows machines that are vulnerable to Meltdown and Spectre at the OS and Hardware level, we built a Kaseya ...

See more from this author →

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK


Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Geschäftsführer: Mel Stephenson

Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072


Love our blog? You should see our emails. Sign up for our newsletter!