BigQuery - 替换字符串中的空格

Ban*_*ter 4 google-bigquery

我的头发在这里变白了。我有一个表,其中有一列我想删除其中的空格。预期结果:“8 800 000 kr”->“8800000 kr”

我无法让它发挥作用。请参阅下面的查询输出:

在此输入图像描述

为什么空白没有被删除?

当我对模拟数据执行查询时,它起作用:

在此输入图像描述

第一个示例中的字符串可能有什么问题?无论我看哪里,我看起来都与示例 2 中的模拟字符串相同。

Mik*_*ant 7

以下示例适用于 BigQuery 标准 SQL,并解释了您的问题

#standardSQL
with `project.dataset.table` as (
  select 'with spaces' space_type, '8 800 000 kr' slutpris union all
  select 'with non-breaking spaces', replace('8 800 000 kr', chr(32), chr(160)) slutpris
)
select space_type, slutpris,
  replace(slutpris, ' ', ''),
  regexp_replace(slutpris, r'\s', ''),
  regexp_replace(slutpris, r'\s|kr', '') 
from `project.dataset.table`    
Run Code Online (Sandbox Code Playgroud)

带输出

在此输入图像描述

因此,正如您所看到的 - 不间断空格不会被识别为空格字符或任何空格

忘记提及可能的解决方案 -

#standardSQL
with `project.dataset.table` as (
  select 'with spaces' space_type, '8 800 000 kr' slutpris union all
  select 'with non-breaking spaces', replace('8 800 000 kr', chr(32), chr(160)) slutpris
)
select space_type, slutpris,
  translate(slutpris, chr(32) || chr(160), ''),
  regexp_replace(slutpris, '[\u00A0\\s]', ''),
  regexp_replace(slutpris, '[\u00A0\\s]|kr', '')
from `project.dataset.table` 
Run Code Online (Sandbox Code Playgroud)

带输出

在此输入图像描述