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:

LTRIM(RTRIM([SKU]))

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!

Source: MSDN – PATINDEX

SIGMA RAPID START

Switching your analytics to Sigma?
Speed up success with a Rapid Start!

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
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
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!