如果替换字符串模式则为雪花

时间:2020-09-21 15:54:51

标签: sql snowflake-cloud-data-platform

我有一个只能包含数字[0-9]的列,但是在某些情况下,我们开始在该列中看到字母数字[eg:p8eSadfghrc]值。 我想写一个条件,如果值不是完全数字{0-9},我想用另一列中的另一个值替换它。

2 个答案:

答案 0 :(得分:1)

像这样吗?

update t
    set col = <other value>
    where regexp_like(col, '[^0-9]');

这将更新数据。您也可以在查询中执行此操作:

select t.*,
       (case when regexp_like(col, '[^0-9]') then <other value> else col end)
from t;

答案 1 :(得分:0)

在雪花中,我建议使用try_to_decimal()。它将尝试将数字转换为十进制值(您可以通过参数控制目标精度和小数位数),如果失败,则返回null

select t.*, case when try_to_decimal(mycol) is null then myothercol else mycol end newcol
from mytable

如果要使用update语句:

update mytable
set mycol = myothercol
where try_to_decimal(mycol) is null

在不提供第二和第三个参数的情况下,允许的位数为38,带有0个小数(这似乎是您想要的)。