Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need to select first part of the string and eliminate rest after a character using REGEXP_SUBSTR

Security Compliance - P2 - United States-Region B
Sales Leadership - Sales Strategy - E1 - United States-Region A
Site Reliability Engineering - M6 - United Kingdom

for example my data in a column looks somewhat like above, i want to use regex_substr() to eliminate everything after P2, E1, and M6 etc which are in the format of 1single capital alphabet and 1 number and its only two characters.

final output looks something like this

Security Compliance
Sales Leadership - Sales Strategy 
Site Reliability Engineering 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would phrase this using a regex replacement:

SELECT col, REGEXP_REPLACE(col, ' - [A-Z][0-9] -.*', '') AS col_out
FROM yourTable;

This approach has an advantage over a substring approach in the event that any column value does not have the matching pattern, in which case the original value would be selected.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading