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')
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.
BigQuery supports casting to NUMERIC. The expression parameter can represent an expression for these data types: INT64. FLOAT64.
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.
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
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`
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`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With