如何用雪花中的空格替换包含字母和特殊字符的行

时间:2020-09-23 14:04:02

标签: sql string sql-update snowflake-cloud-data-platform snowsql

我有一个“ A”列,其中包含数字,例如-0001、0002、0003

同一列“ A”在某些行中还包含一些字母和特殊字符,例如-connn,cco * jjj,hhhhhh11111等。

我想将这些字母和特殊字符行替换为空白值,只希望保留包含数字的行。

我可以在此处使用哪个正则表达式?

2 个答案:

答案 0 :(得分:0)

我了解到您希望将所有不只包含数字的值设置为null

如果是这样,您可以使用try_to_decimal()

update mytable
set a = null
where a try_to_decimal(a) is null

或正则表达式匹配:

update mytable
set a = null
where a rlike '[^0-9]'

答案 1 :(得分:0)

如果您想从这些值中提取数字(即使它们以非数字结尾或以非数字开头),则可以使用类似以下的方法:

create table testing ( A varchar ) as select *
from values ('123'),('aaa123'),('3343'),('aaaa');

select REGEXP_SUBSTR( A, '\\D*(\\d+)\\D*', 1, 1, 'e', 1 ) res 
from testing;

+------+
| RES  |
+------+
| 123  |
| 123  |
| 3343 |
| NULL |
+------+