1

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?

Many thanks!

2 Answers 2

1

Regular expressions aren't really needed:

UPDATE photo
    SET filepath = 'con' || substring(filepath, 4)
WHERE filepath LIKE 'cor/%'
Sign up to request clarification or add additional context in comments.

1 Comment

Clever way to do it :) Thank you very much.
1

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/';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.