如何用雪花中的空格替换包含字母和特殊字符的行
How to replace rows containing alphabets and special characters with Blank spaces in snowflake
我有一列“A”,其中包含数字,例如 - 0001、0002、0003
同一列“A”在某些行中还包含一些字母和特殊字符,例如 - connn、cco*jjj、hhhhhh11111 等
我想用空白值替换这些字母和特殊字符行,并且只想保留包含数字的行。
我可以在这里使用哪个正则表达式?
我了解到您想将所有不包含数字的值设置为 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]'
如果你想从这些值中提取数字(即使它们以非数字结尾或开头),你可以使用这样的东西:
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 |
+------+
我有一列“A”,其中包含数字,例如 - 0001、0002、0003
同一列“A”在某些行中还包含一些字母和特殊字符,例如 - connn、cco*jjj、hhhhhh11111 等
我想用空白值替换这些字母和特殊字符行,并且只想保留包含数字的行。
我可以在这里使用哪个正则表达式?
我了解到您想将所有不包含数字的值设置为 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]'
如果你想从这些值中提取数字(即使它们以非数字结尾或开头),你可以使用这样的东西:
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 |
+------+