How can I automatically extract content from a field in a SQL query?

Viewed 53

The environment I am currently working in is Snowflake. As a matter of data sensitivty, I will be using pseudonyms for my following question.

I have a specific field in one of my tables called FIELD_1. The data in this field is structured as such:

enter image description here

I am trying to figure out how to automatically extract from my FIELD_1 the output I have in FIELD_2.

Does anyone have any idea what kind of query I would need to achieve this? Any help would be GREATLYappreciated! I am really quite stuck on this problem.

Thank you!

2 Answers

You seem to want everything up to the first four numbers. Then to replace the underscores with spaces. If so:

select replace(regexp_substr(field_1, '^[^0-9]*[0-9]{4}'), '_', ' ')

Or alternatively, if you want the first three components separated by underscores:

select replace(regexp_substr(field_1, '^[^_]+_[^_]+_[0-9]{4}'), '_', ' ')
Related