Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery: SPLIT() returns only one value

I have a page URL column components of which are delimited by /. I tried to run the SPLIT() function in BigQuery but it only gives the first value. I want all values in specific columns.

I don't understand how to use the Regexp_extract() example mentioned in Split string into multiple columns with bigquery.

I need something similar to REGEX_SPLIT_TO_TABLE(<String>, <DELIMITER>) which converts a single string into multiple columns.

Query:

SELECT PK, 
DATE(TIMESTAMP(CONCAT(SUBSTR(date,1,4),'-',SUBSTR(date,5,2),'-',SUBSTR(date,7,2),' 00:00:00'))) as visit_date,
hits_page_pagePath,
split(hits_page_pagePath,'/')
FROM [Intent.All2mon] limit 100
like image 740
Saumil Agrawal Avatar asked Nov 21 '14 11:11

Saumil Agrawal


People also ask

How do I split text in BigQuery?

Using SPLIT(value[, delimiter]) returns an array. Then using SAFE_OFFSET(zero_based_offset) or SAFE_ORDINAL(one_based_offset) to get item from array. SELECT SPLIT(app_info.

How does coalesce work in BigQuery?

COALESCE. Returns the value of the first non-null expression. The remaining expressions are not evaluated. An input expression can be any type.

What is regex in BigQuery?

Regular expression in bigquery used to validate the query used in Google Cloud Platform(GCP). Regex specifies a search pattern for query used in bigquery services. While using this service, users have to validate the query to get the desired result. Regex is the best option to check your query.


1 Answers

2018 standardSQL update:

#standardSQL
SELECT SPLIT(path, '/')[OFFSET(0)] part1,
       SPLIT(path, '/')[OFFSET(1)] part2,
       SPLIT(path, '/')[OFFSET(2)] part3
FROM (SELECT "/a/b/aaaa?c" path)

Now I understand you want them in different columns.

An alternative to the query you provided:

SELECT FIRST(SPLIT(path, '/')) part1,
       NTH(2, SPLIT(path, '/')) part2,
       NTH(3, SPLIT(path, '/')) part3
FROM (SELECT "/a/b/aaaa?c" path)

NTH(X, SPLIT(s)) will provide the Xth value from the SPLIT. FIRST(s) is the same as NTH(1, s)

like image 154
Felipe Hoffa Avatar answered Sep 17 '22 17:09

Felipe Hoffa