Skip to content
Advertisement

Split a single sql column into five

I’m trying to split one column into up to five around the ” > ” delimiter but the things I’ve tried haven’tw orked:

I tried

select
id, 
compoundColumn,
split(compoundColumn," > ")[1] as "first"
split(compoundColumn," > ")[2] as "second"
from table
where compoundColumn is not null

which didn’t work, and

this which sort of did (the first part anyway, not the nth part)

select
id, 
compoundColumn,
first(split(compoundColumn," > ")) as "first"
nth(compoundColumn," > ")[n] as "second"
from table

I’ve found lots of examples on here but they all seem to be saying to use the brackets but the brackets throw an error:

Exception: Malformed SQL. More information: Error with SQL statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘[1] as “first” from table where compoundColumn IS NOT NULL’ at line 3.

Advertisement

Answer

I finally got where I needed to go using regexp extract, in the bigquery pull rather than within appmaker:

SELECT 
  CompoundColumn,

  REGEXP_EXTRACT(CompoundColumn+">",  r'^(.*?)>') first_number,
  REGEXP_EXTRACT(CompoundColumn+">",  r'^(?:(?:.*?)>){1}(.*?)>') second_number,
  REGEXP_EXTRACT(CompoundColumn+">", r'^(?:(?:.*?)>){2}(.*?)>') third_number,
  REGEXP_EXTRACT(CompoundColumn+">",  r'^(?:(?:.*?)>){3}(.*?)>') fourth_number
FROM
  myTable
WHERE
  CompoundColumn IS NOT NULL

The +”>” part of the code is ugly but I couldn’t get it to match strings that didn’t end with the bracket (“>?” breaks the whole thing) so I just made them all end with the bracket.

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