Skip to content
Advertisement

Find matching pattern

I have a table that contains multiple strings pattern (‘Q88 9WE’,’S9 D2P’,WC2R 3LS etc..). How do I extract and split the rows that meet only the pattern

Where A is a character in the range A-Z and N is a digit 1-9

I have tried using the logic below and it doesn’t seem to work

I’m trying to map each pattern to a number and I will later put in a cte so I can do a select * from cte where mapping = 1. I’m having difficulty creating a case logic for multiple digit scenario.

Advertisement

Answer

Same idea as Gordon’s post, but with a bit of code reuse to reduce chance of typos when entering a new pattern:

If you wanted to use your original approach, it wasn’t working for a couple of reasons:

  1. One of your substrings was searching for an empty string (”) rather than a space (‘ ‘)
  2. Your code was inconsistent: the first number search used PatIndex nested inside Substring (achieves desired result), but in the 2nd usage (looking at the 4th character), you missed off the Substring function (searches for something different, breaking the query).

With those errors corrected:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement