开发者

Postgres: edit values with regex?

开发者 https://www.devze.com 2023-03-06 11:07 出处:网络
I\'d like to run a query in postgres that finds all rows in a table with a filepath of cor/* and sets them to con/*.

I'd like to run a query in postgres that finds all rows in a table with a filepath of cor/* and sets them to con/*.

In pseudocode:

UPDATE photo set filepath="con/*" where filepath="cor/*";

Please could anyone help me out with the correct postgres syntax? Is this possible in postgres?

开发者_如何学JAVA

Many thanks!


Regular expressions aren't really needed:

UPDATE photo
    SET filepath = 'con' || substring(filepath, 4)
WHERE filepath LIKE 'cor/%'


There's a regexp_replace() function:

http://www.postgresql.org/docs/current/static/functions-string.html

update photo
set filepath = regexp_replace(filepath, '^cor/', 'con/')
where filepath ~ '^cor/';
0

精彩评论

暂无评论...
验证码 换一张
取 消