仅使用数字修剪十进制值


Jacob
2025-03-17 08:48:01 (21天前)


我的表中有很少的字段,包括V1.5,1.6,V1.6.7。我想删除所有字符并仅保留表中的数字。
V1.5应该改为1.5
V1.6.7应改为1.6.7
1.6应该……

2 条回复
  1. 0# 易米烊光 | 2019-08-31 10-32



    要删除不是数字或点的所有内容,您可以使用

    regexp_replace()





    1. regexp_replace(the_column, ‘[^0-9.]’, ‘’, g’)

    2. </code>


    例如




    1. 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;

    2. </code>


    收益:




    1. 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

    2. </code>


    要更改表中的数据,请使用UPDATE语句:




    1. update the_table
      set the_column = regexp_replace(the_column, ‘[^0-9.]’, ‘’, g’)
      where the_column <> regexp_replace(the_column, ‘[^0-9.]’, ‘’, g’);

    2. </code>




    where

    子句阻止不更改任何内容的更新。



    当然,你需要更换

    the_column

    使用您想要更改的真实列名称。


登录 后才能参与评论