Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get everything left of a certain set of characters in BigQuery SQL?

So what I have is a series of Strings all have a distinct 'MIN' in each string, for example:

abcdefMINjklkkase
ffffffffffMINxxxxxxxxxx
eeeMINoooooooooooooooooo
ggggggggMINkkkkkkk
wwwwwwwwwwwwwwwwMINiiiiiiii

All String sets are different length otherwise I would use LEFT function or LTRIM functions. The result I would like to get is,

abcdef
ffffffffff
eee
gggggggg
wwwwwwwwwwwwwwww

The variable, 'MIN' is consistent in all string sets just at various different lengths due to how the Strings are created.

Any help would be much appreciated!

-Maykid

like image 908
Maykid Avatar asked Dec 10 '22 06:12

Maykid


1 Answers

#standardSQL
SELECT 
  str, 
  REGEXP_REPLACE(str, r'MIN.*', '') option_1,
  REGEXP_EXTRACT(str, r'(.*?)MIN.*') option_2
FROM `project.dataset.table`
like image 182
Mikhail Berlyant Avatar answered May 03 '23 07:05

Mikhail Berlyant