In concert with writing quality open-source SQL blog posts, I have developed a yearning to become a well-versed PostgreSQL PLpgSQL programmer. What a perfect opportunity to share what I learn about PLpgSQL, on my blog, while building solid SQL skills. In this post, I will cover the SELECTINTO clause as part of a PLpgSQL function, focusing on the STRICT keyword for even more fine-tuned functionality.
Start talking about dollars, and you will get all the attention in the world. Well, in this instance, it is not so much about dollars in the monetary sense, but more so in a programming one. You’re likely wondering just what on earth I am talking about. Read on and learn with me about a neat feature that PostgreSQL’s PLpgSQL procedural language provides when creating functions and stored procedures.
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.
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.
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->WHERE rating ='G'
dvdrental->LIMIT1; replace ---------
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->WHERE rating ='NC-17'
dvdrental->LIMIT1; replace -------------------
Non Compatible-17 (1row)
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!!!
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.
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.
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…
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…
On this website we use first or third-party tools that store small files (cookie) on your device. Cookies are normally used to allow the site to run properly (technical cookies), to generate navigation usage reports (statistics cookies) and to suitable advertise our services/products (profiling cookies). We can directly use technical cookies, but you have the right to choose whether or not to enable statistical and profiling cookies. Enabling these cookies, you help us to offer you a better experience.