Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining string after Last Slash in BigQuery Standard SQL

Let's say I have a column called 'Youtube' and I want to extract the string after the last slash of a URL. How would I do this in BigQuery Standard SQL?

Examples:

https://youtube.com/user/HaraldSchmidtShow

https://youtube.com/user/applesofficial

https://youtube.com/user/GrahamColton

Essentially, I want:

HaraldSchmidtShow

applesofficial

GrahamColton
like image 834
javacash Avatar asked Oct 30 '18 21:10

javacash


People also ask

How do I select part of a string in BigQuery?

BigQuery substring after character For this, the first step is to use the STRPOS() function inside the SUBSTR() function to define the precise location of the @ character in the string. Afterwards we add 1 to the substring to indicate where the beginning is.

What is coalesce in BigQuery?

COALESCE(expr[, ... ]) Description. Returns the value of the first non- NULL expression. The remaining expressions aren't evaluated.

How do you get a remainder in BigQuery?

One of the most popular BigQuery math functions is the Modulo function. It simply gives you the remainder from the division of X by Y. The value that it returns carries the same sign as X.

How do you escape single quote in BigQuery?

Quoted literals: Quoted strings enclosed by single ( ' ) quotes can contain unescaped double ( " ) quotes, as well as the inverse. Backslashes ( \ ) introduce escape sequences.


2 Answers

An alternative to the previous answer, which also works when there's a '/' at the end:

WITH data AS(
  SELECT 'https://youtube.com/user/HaraldSchmidtShow' AS url UNION ALL
  SELECT 'https://youtube.com/user/applesofficial' UNION ALL
  SELECT 'https://youtube.com/user/GrahamColton' UNION ALL
  SELECT 'https://youtube.com/user/GrahamColton/'
)

SELECT REGEXP_EXTRACT(url, r'/([^/]+)/?$') name
FROM `data`
like image 158
Felipe Hoffa Avatar answered Sep 28 '22 03:09

Felipe Hoffa


This might already do the trick for you:

WITH data AS(
  SELECT 'https://youtube.com/user/HaraldSchmidtShow' AS url UNION ALL
  SELECT 'https://youtube.com/user/applesofficial' UNION ALL
  SELECT 'https://youtube.com/user/GrahamColton'
)

SELECT
  SPLIT(url, '/')[SAFE_OFFSET(ARRAY_LENGTH(SPLIT(url, '/')) - 1)] AS name
FROM `data`

It just splits the string and goes for the last value.

like image 22
Willian Fuks Avatar answered Sep 28 '22 05:09

Willian Fuks