0

I have a table in BigQuery with a variable with strings of text like this:

row  path
1    /998777/kkjs/lksjaflkjsdf/sdfñklñ{sdf
2   /ñljasdfkgbsdf/5854654/lsjflkjsdf/asdfasdfsdf
3    /11544/sdfsdf/asdfsdfasdfdsf/sssfdsfdsdf

How can I query this table to extract the numbers for the variable so I get a new variable with:

row  path2
1    998777
2    5854654
3    11544

Thx!

0

2 Answers 2

1

Below example for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '/998777/kkjs/lksjaflkjsdf/sdfñklñ{sdf' path UNION ALL
  SELECT '/ñljasdfkgbsdf/5854654/lsjflkjsdf/asdfasdfsdf' UNION ALL
  SELECT '/11544/sdfsdf/asdfsdfasdfdsf/sssfdsfdsdf' 
)
SELECT path, REGEXP_EXTRACT(path, r'/(\d+)/') path2
FROM `project.dataset.table`

with result

Row path                                            path2    
1   /998777/kkjs/lksjaflkjsdf/sdfñklñ{sdf           998777   
2   /ñljasdfkgbsdf/5854654/lsjflkjsdf/asdfasdfsdf   5854654  
3   /11544/sdfsdf/asdfsdfasdfdsf/sssfdsfdsdf        11544    
Sign up to request clarification or add additional context in comments.

1 Comment

Edited the question as I only want to retain full set of numbers among // al the rest should be excluded
0

You can use regexp_replace():

select regexp_replace(str, '[^0-9]', '')

2 Comments

This keeps all numbers however there are numbers among the rest of the string such as: /998777/kkjs/lksjaflkjsdf/sdfñklñ9ddsdf/99h/ so this includes 99 as well which is not the expected behaviour
@EGM8686 . . . There are none in your sample strings and it is not clear if you want to keep all digits or just specific digits. If specific digits, you should be clear on what the rules are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.