REPLACE() string function – with examples in PostgreSQL

String functions are quite useful for manipulating character and text data. Most SQL dialects have several different ones for different use cases so there is surely one (or two) to fit your needs. In this post, I’ll visit the REPLACE() function with examples in PostgreSQL.

Photo by Jason Leung on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:
  • Xubuntu Linux 18.04.2 LTS (Bionic Beaver)
  • PostgreSQL 11.2


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


I’ll use a couple of tables from the PostgreSQL practice DVD Rental database for the example queries below.

A while back, I wrote Useful String Function – REPLACE in MySQL with examples. I feel it’s only fair to provide the same for the PostgreSQL fanboy (in me) and like-minded fans of this great ecosystem as well.

Let’s see what values are present in the ‘film’ table’s ‘rating’ column:

1
2
3
4
5
6
7
8
9
dvdrental=> SELECT DISTINCT rating FROM film;
 rating
--------
 PG
 R
 G
 PG-13
 NC-17
(5 rows)

If you want to change ‘PG’ to ‘Parental Guidance’ you can pass in that argument to REPLACE():

1
2
3
4
5
6
7
8
dvdrental=> SELECT REPLACE(rating::text, 'PG', 'Parental Guidance')
dvdrental-> FROM film
dvdrental-> WHERE rating = 'PG'
dvdrental-> LIMIT 1;
      replace      
-------------------
 Parental Guidance
(1 row)

As you can see, the syntax is easy to follow. Provide: 1) the target string, 2) the portion of the target string you want to be replaced, 3) what you want that portion replaced with. Then, you’re off to the races.

Here’s another simple example along the same lines as that shown above:

1
2
3
4
5
6
7
8
dvdrental=> SELECT REPLACE(rating::text, 'G', 'General')
dvdrental-> FROM film
dvdrental-> WHERE rating = 'G'
dvdrental-> LIMIT 1;
 replace
---------
 General
(1 row)

To be honest, I am not quite sure what the NC stands for in ‘NC-17’, but I’ll wing it anyways:

1
2
3
4
5
6
7
8
dvdrental=> SELECT REPLACE(rating::text, 'NC-17', 'Non Compatible-17')
dvdrental-> FROM film
dvdrental-> WHERE rating = 'NC-17'
dvdrental-> LIMIT 1;
      replace      
-------------------
 Non Compatible-17
(1 row)

Try out the REPLACE() function and see how you like it or where you can put it to good use. Hit me up in the comments below with your feedback.

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official PostgreSQL 11 On-line Documentation for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Strings from date values with the TO_CHAR() function in PostgreSQL.

Oftentimes, you may need to convert a date value or one of its parts to a string representation. Be it reporting, visualization for ad-hoc querying, or a myriad of other things; this functionality is handy at the least. Among other numeric types, the to_char function accepts date/time values with an additional parameter of the desired string character value from said type. Let’s learn together with some simple examples below…

[Head this way for great PostgresSQL blogging >>>]

LAG() Window Function – with examples in PostgreSQL.

Having recently covered the LEAD() Window Function here on Digital Owl’s Prose, it only makes sense to follow-up with a post on its counterpart: LAG(). By all means, don’t lag behind. Read on and learn something with me…

[Head this way for great PostgresSQL blogging >>>]

LEAD() Window Function – with examples in PostgreSQL

Having been forewarned in several of my previous blog posts that I am on a SQL Window Function kick, it should be no surprise that here I am again; sharing what I am studying and learning. I continue to find these powerful constructs highly interesting, to say the least. In this post, I visit the LEAD() Window Function with examples in PostgreSQL. Read on and let’s learn something together…

[Head this way for great PostgresSQL blogging >>>]

Window Function windowing options – with examples in PostgreSQL

More window functions here on the blog? What can I say? The deeper I get into them, the more I realize their power. Yet, I have not covered any of the Windowing options available in the OVER() clause so far and that ends with this post. I will share what I have learned, and what I am still learning. Join me and we can learn together…

[Head this way for great PostgresSQL blogging >>>]