要删除不是数字或点的所有内容,您可以使用
regexp_replace()
:
regexp_replace(the_column, ‘[^0-9.]’, ‘’, ‘g’)
</code>
例如
with data (version) as (
values
(‘V1.5’),
(‘V1.6.7’),
(‘1.2.3’),
(‘3.4.5-alpha’)
)
select version, regexp_replace(version, ‘[^0-9.]’, ‘’, ‘g’) as clean_version
from data;
</code>
收益:
version | clean_version
——————+———————
V1.5 | 1.5
V1.6.7 | 1.6.7
1.2.3 | 1.2.3
3.4.5-alpha | 3.4.5
</code>
要更改表中的数据,请使用UPDATE语句:
update the_table
set the_column = regexp_replace(the_column, ‘[^0-9.]’, ‘’, ‘g’)
where the_column <> regexp_replace(the_column, ‘[^0-9.]’, ‘’, ‘g’);
</code>
该
where
子句阻止不更改任何内容的更新。
当然,你需要更换
the_column
使用您想要更改的真实列名称。