Postgres – Update table data by replacing words

While working with some email templates, I made a typo. Now, I had a few options to choose from. Open each template from the UI and make the change. The other one was use a SQL editor and create update commands, modify the data in the query and execute. But either of those would be quite laborious. Then I searched for something that could do an update with a string replace across all fields in a table and Voila, found it. Here’s the solution:

UPDATE commander_commonemailtemplate 
SET email_body = replace(email_body, 'file from the links at the bottom', 'file from the link at the bottom');

As you noticed, links was supposed to be replaced with link. But the reason I chose a bigger line was so that if if there are other rightful occurrences of the word links, they won’t get replaced.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s