Skip to content
Advertisement

snowflake substring by pattern

I have a string column in my table as below:

i need a dynamic way of only extracting 123456 from these string. Can you plz suggest a solution.

Advertisement

Answer

Use the REGEXP_SUBSTR(…) built-in function to extract substrings using a regular expression pattern.

If there will only be one number in each column value, a number pattern or a numeric characters range syntax will suffice:

If the number will be exactly 6 digits wide, use the {n} repetition syntax:

If the number must only follow the text accountNumber, you can introduce (capture groups):

Building a fully correct regular expression will require more knowledge about all of the variances possible in the data. Try building out your patterns interactively with a good test-set on sites such as Regex101, RegExr, etc. that make it easier to develop them.

Note: If your data is actually in a JSON format throughout, Snowflake permits parsing them into a VARIANT data type to query them more naturally:

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