Tuesday, November 12, 2019

SQl Server SSMS - Get letters from beginning of string

I had a requirement, in SSMS, where I needed to get the first letters of a Voucher string. It could be anywhere from 3-6 letters so I couldn't hard code a start and end point.

I ended up coming up with this:

SUBSTRING([MyCol], 1, PATINDEX('%[^a-z]%', [MyCol]) - 1)

I was able to put a start of '1' as my Voucher letters ALWAYS come at the beginning.  The 'PATINDEX' finds the column AFTER my letters end.  So, I subtract 1 and I've found the end!

No comments:

Post a Comment