BigQuery-将字符串转换为数字

问题描述

我正在寻找 RE2 接受的正则表达式语法, 将以下字符串转换为数字:

“ 339,840”-> 339840

“ $ 100,000”-> 100000

“ 0.75”-> 0.75

“ 1”-> 1

解决方法

以下是用于BigQuery标准SQL

您可以使用cast(regexp_replace(val,r'[^\d.]','') as numeric)

请参见以下示例

#standardSQL
with `project.dataset.table` as (
  select "339,840" val union all
  select "$100,000" union all
  select "0.75" union all
  select "1" 
)
select cast(regexp_replace(val,'') as numeric)
from `project.dataset.table`

有输出

enter image description here