bigquery sql 中的数据清理从浮点数变为整数
Data cleaning in bigquery sql changing from float to integer
您好,我有一个 table,我想在其中将列中的值从浮点数更改为整数并替换值并将其保留为字符串。我最好怎么做?
table如下:
id
discount
product
1
10
dettol soap
2
10.0
dettol soap green
3
0
dettol soap red
4
x
dettol soap blue
5
dettol soap yellow
6
5
dettol soap beige
9
13.3210282172312131
dettol soap white
我想把折扣table刷成一样table
id
discount
product
1
10
dettol soap
2
10
dettol soap green
3
0
dettol soap red
4
0
dettol soap blue
5
0
dettol soap yellow
6
5
dettol soap beige
9
13
dettol soap white
我有的是
select
ID
, cast(cast(discount as integer) as string)
, 产品
来自 table
如何将非数字和空格替换为 0,将小数替换为整数并转换为字符串并将值替换为当前 table?
考虑一下:
SELECT CAST(IFNULL(CAST(SAFE_CAST(x AS FLOAT64) AS INT64), 0) AS STRING) AS discount
FROM UNNEST(['10', '10.0', '0', 'x', '', '5', '13.3210282172312131']) x;
或者,你可以用正则表达式试试。
SELECT IFNULL(REGEXP_EXTRACT(x, r'^[0-9]+'), '0') discount
FROM UNNEST(['10', '10.0', '0', 'x', '', '5', '13.3210282172312131']) x;
输出:
考虑以下
select *
replace(
format('%.0f', ifnull(safe_cast(discount AS float64), 0))
as discount)
from your_table
如果应用于您问题中的示例数据 - 输出为
您好,我有一个 table,我想在其中将列中的值从浮点数更改为整数并替换值并将其保留为字符串。我最好怎么做?
table如下:
id | discount | product |
---|---|---|
1 | 10 | dettol soap |
2 | 10.0 | dettol soap green |
3 | 0 | dettol soap red |
4 | x | dettol soap blue |
5 | dettol soap yellow | |
6 | 5 | dettol soap beige |
9 | 13.3210282172312131 | dettol soap white |
我想把折扣table刷成一样table
id | discount | product |
---|---|---|
1 | 10 | dettol soap |
2 | 10 | dettol soap green |
3 | 0 | dettol soap red |
4 | 0 | dettol soap blue |
5 | 0 | dettol soap yellow |
6 | 5 | dettol soap beige |
9 | 13 | dettol soap white |
我有的是 select ID , cast(cast(discount as integer) as string) , 产品 来自 table
如何将非数字和空格替换为 0,将小数替换为整数并转换为字符串并将值替换为当前 table?
考虑一下:
SELECT CAST(IFNULL(CAST(SAFE_CAST(x AS FLOAT64) AS INT64), 0) AS STRING) AS discount
FROM UNNEST(['10', '10.0', '0', 'x', '', '5', '13.3210282172312131']) x;
或者,你可以用正则表达式试试。
SELECT IFNULL(REGEXP_EXTRACT(x, r'^[0-9]+'), '0') discount
FROM UNNEST(['10', '10.0', '0', 'x', '', '5', '13.3210282172312131']) x;
输出:
考虑以下
select *
replace(
format('%.0f', ifnull(safe_cast(discount AS float64), 0))
as discount)
from your_table
如果应用于您问题中的示例数据 - 输出为