In a previous post, I explored using both the COPY command and the CAST() function together in order to upload an entire CSV file’s data into a PostgreSQL database table. This post is a continuation, of sorts. However, I will use Python and the psycopg2 library, in a simple script, to handle these uploads instead of SQL. While I am still a novice with operations such as these, I feel that writing about it is a fantastic way to learn and obtain valuable feedback…
Loading data into database tables is pretty much a necessity. Without data, what do we have? Not much at all. The CSV format is super common, used far and wide. I keep a CSV file of my daily walking/hiking stats and am looking to store them in a PostgreSQL database on my local learning/development machine. How can I load a CSV – with several rows of data – at one go in Postgres? What about data types? Any concerns there? Continue reading to see a simple, yet effective solution…
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->FROM film dvdrental->WHERE rating ='G' dvdrental->LIMIT1; replace --------- General (1row)
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->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…
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.