Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a BigQuery version of isnumeric

I need to test if a field is numeric or not using standard SQL in BigQuery.

The example below works and is similar to what I have done in Cognos using TRANSLATE('mystring','1234567890.','') but its not very elegant.

SELECT
IF(LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE('1234.56','1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''),'0',''),'.',''))=0,
'A number',
'Not a number')
like image 793
Steve Avatar asked Aug 29 '18 14:08

Steve


People also ask

What is the equivalent of BigQuery?

Amazon Redshift As the most proven tool in this category, Amazon Redshift is a fully managed cloud-based data warehouse used to collect and store data. Like BigQuery, Redshift seamlessly integrates with multiple products and ETL services. With Redshift, data is stored in the cloud for seamless analysis.

Is Numeric in BigQuery?

BigQuery supports casting to NUMERIC. The expression parameter can represent an expression for these data types: INT64. FLOAT64.

What is the difference between float and numeric in BigQuery?

The main difference is Floats / Doubles are binary floating point types and a Numeric will store the value as a floating decimal point type. So Numeric have much higher precision and are usually used within monetary (financial) applications that require a high degree of accuracy.


3 Answers

You can use SAFE_CAST to try casting to a number. SAFE_CAST casts similar to CAST, but if casting fails, instead of erring null is returned.

For example you can do:

SAFE_CAST('1234567890' AS FLOAT64);

which will return 1.23456789E9

like image 129
alamoot Avatar answered Jan 04 '23 01:01

alamoot


Thanks for both suggestions, both work a treat and I have gone for the SAFE_CAST option as it runs a fraction quicker.

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '1234.56' col UNION ALL
  SELECT '1234.' col UNION ALL
  SELECT '1234' col UNION ALL
  SELECT '.56' col UNION ALL
  SELECT '1234..56' col UNION ALL
  SELECT 'a1234.56' 
 )
 SELECT
   col,
   if(SAFE_CAST(col AS FLOAT64) is null,'Not a number', 'A number')
 FROM `project.dataset.table`
like image 31
Steve Avatar answered Jan 04 '23 01:01

Steve


but its not very elegant

Below examples for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '1234.56' col UNION ALL
  SELECT '1234.' col UNION ALL
  SELECT '1234' col UNION ALL
  SELECT '.56' col UNION ALL
  SELECT '1234..56' col UNION ALL
  SELECT 'a1234.56' 
)
SELECT
  col,
  IF(LENGTH(REGEXP_REPLACE(col, r'[\d.]', '')) = 0, 'A number', 'Not a number') ,
  IF(REGEXP_CONTAINS(col, r'^\d*.?\d*$'), 'A number', 'Not a number') 
FROM `project.dataset.table`
like image 40
Mikhail Berlyant Avatar answered Jan 04 '23 00:01

Mikhail Berlyant