Postgres regexp_replace查询用法



我们有一个列,其前缀值后跟动态数字,例如

AAAA0000
AAAA0001
AAAA0002
AAAA0003
... 
...

现在我们想要在它存在的所有行中将前缀值从AAAA更新为BBBB。我尝试使用regexp_replace, replace和其他可能的功能,但没有成功。

你能帮我做这件事吗?

update table_name set the_column = 'BBBB'||substr(the_column, 6,13) where the_column like 'AAAA%';

其中as, 6为数字的起始位置,13为字符串的结束位置。因此,值'BBBB'将被更新,直到位置5,然后是上面提取的子字符串的连接。

我不认为这里需要正则表达式:

update the_table
  set the_column = 'BBBB'||substr(the_column, 5)
where the_column like 'AAAA%';

regexp_replace的用法如下:

update the_table
   set the_column = regexp_replace(the_column, '^AAAA', 'BBBB');

最新更新