简体   繁体   中英

How Many Numbers Are In A BigQuery Field That Is A String

I want to simply count how many numbers are in a string inside BigQuery.

I can obviously count how long the string is using LENGTH():

DECLARE str STRING DEFAULT 'h0w many numb3rs?';
SELECT
  str,
  LENGTH(str) AS length;

But how do I count how many numbers are in the string?

Ok, you guys got me:o)
Joining this thread with below simple and cheap option

DECLARE str STRING DEFAULT 'h0w many numb3rs?';
SELECT str, LENGTH(str) length, COUNT(*) digits
FROM UNNEST(SPLIT(str, '')) char
WHERE char BETWEEN '0' AND '9'    

with output

在此处输入图像描述

Extract only numeric values from the string using regex and count them

DECLARE str STRING DEFAULT 'h0w many numb3rs?';
SELECT
  str,
  LENGTH(str) AS length, array_length(regexp_extract_all(str, r'[\d]'))

在此处输入图像描述

reference:

https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#regexp_extract_all

https://cloud.google.com/bigquery/docs/reference/standard-sql/array_functions#array_length

You may consider below approach wherein you can directly count the length of the numbers from REGEXP_REPLACE .

DECLARE str STRING DEFAULT 'h0w many numb3rs?';
SELECT
  str,
  LENGTH(str) AS length,
  LENGTH(regexp_replace(str, r'[^0-9]', '')) as total_numbers

OUTPUT:

在此处输入图像描述

The trick is to remove numbers (by replacing numbers with empty strings using REGEXP_REPLACE) and then find the difference in the original string length and the reduced string length. Search the re2 documentation for available regex syntax. You can use the Perl shorthand [\d] for digits (or [0-9] works too).

DECLARE str STRING DEFAULT 'h0w many numb3rs?';
SELECT
  str,
  LENGTH(str) AS length,
  LENGTH(str) - LENGTH(REGEXP_REPLACE(str, r'[\d]', '')) AS numbers,
  LENGTH(str) - LENGTH(REGEXP_REPLACE(str, r'[[:alpha:]]', '')) AS non_alpha_characters;

(note: you can find the non-alpha characters (including spaces and punctuation) by matching and removing [[:alpha:]] (or equivalently [A-Za-z] ) characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM