PostgreSQL replace string
This article discusses how to use PostgreSQL replace()
functions to replace strings.
Use the PostgreSQL replace()
function to replace strings
PostgreSQL replace()
functions have the following arguments, which are all text types:
replace (string text, from text, to text)
string
The argument is replace()
the source text to execute the function. from
The argument is string
a substring of the argument that represents the portion to be changed.
to
Parameter represents the substring of parameter to which parameter from
is to be changed .string
For example, let's use replace()
the function to Catfish
change the word to Turtle
, as follows:
SELECT replace ('Catfish', 'Catfish', 'Turtle');
turn out:
replace
---------
Turtle
Let's replace Catfish
the substring in with , as follows:Cat
Jelly
SELECT replace('Catfish', 'Cat', 'Jelly');
turn out:
replace
-----------
Jellyfish
This function can also be used to replace special characters in a string. For example, replace spaces with commas as follows:
SELECT replace('A B C D E', ' ', ',');
turn out:
replace
-----------
A,B,C,D,E
Suppose we have a text of random characters, and we want to b
replace every occurrence of that character with a
, regardless of whether the character a
is uppercase or lowercase. If we run this command, as shown below:
SELECT replace('agAdavA', 'a', 'b');
turn out:
replace
---------
bgAdbvA
The result shown above does not satisfy the requirement of a
replacing all occurrences of uppercase and lowercase with b
, since only the occurrences of lowercase are replaced a
. Therefore, we have to introduce the PostgreSQL regexp_replace()
function.
regexp_replace()
Use the function to replace strings
in PostgreSQL
PostgreSQL regexp_replace()
functions have the following arguments, which are all text types:
regexp_replace (string text, pattern text, replacement text [,flags text])
string
The argument is the source string to perform the replace function pattern
on. The argument represents a regular expression that must match before a substring of the string argument can be replaced.
replacement
Parameter represents the text that we change the substring of the string parameter to. flags
Parameter represents regexp_replace()
text that can be used to change the behavior of the function.
In the previous example, we need to a
change all occurrences of uppercase and lowercase to b
, we can use regexp_replace()
the function, as follows:
SELECT regexp_replace('agAdavA', '[a | A]+', 'b', 'g');
turn out:
regexp_replace
----------------
bgbdbvb
With the introduction of regular expressions, all occurrences of uppercase and lowercase a
are replaced as necessary. g
The -e flag ensures that all occurrences are replaced, not just the first a
.
Here is the PostgreSQL documentation on pattern matching .
More PostgreSQL replace()
Function Examples
Suppose we have a table with a text
column called consisting of single words, like this:
id | text |
---|---|
1 | Red |
2 | Green |
3 | Blue |
4 | Red |
5 | Red |
We want to Yellow
replace every occurrence of the word with Red
. We can use replace()
the function as follows:
UPDATE demo SET text = replace(text, 'Red', 'Yellow');
turn out:
id | text
----+--------
1 | Yellow
2 | Green
3 | Blue
4 | Yellow
5 | Yellow
Suppose in the same table we text
have words with special characters in the field like this:
id | text |
---|---|
6 | g-r-e-e-n |
7 | 1-23–4 |
8 | one-and-two |
9 | —n—2— |
10 | —– |
We want to replace all occurrences of hyphens ( -
) with underscores ( _
). replace()
A function can do this, as shown below:
UPDATE demo SET text = replace(text, '-', '_');
turn out:
id | text
----+-------------
6 | g_r_e_e_n
7 | 1_23__4
8 | one_and_two
9 | ___n___2___
10 | _____
If we insert more records into the table so that text
the field contains sentences instead of single words, as shown below:
id | text |
---|---|
11 | She bought a red bag |
12 | Green is a good color |
13 | The sky is blue |
14 | The color of the shirt is red |
15 | They plan to go with blue or red balloons |
We want to yellow
replace the word with red
. This can be replace()
achieved using the function as follows:
UPDATE demo SET text = replace(text, 'red', 'yellow');
turn out:
id | text
----+----------------------------------------------
11 | She bought a yellow bag
12 | Green is a good color
13 | The sky is blue
14 | The color of the shirt is yellow
15 | They plan to go with blue or yellow balloons
Next, here are the commands to run:
--CREATE statement
CREATE TABLE demo (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
text TEXT NOT NULL
);
--insert first set of records
INSERT INTO demo (text)
VALUES
('Red'),
('Green'),
('Blue'),
('Red'),
('Red');
--insert second set of records
INSERT INTO demo (text)
VALUES
('g-r-e-e-n'),
('1-23--4'),
('one-and-two'),
('---n---2---'),
('-----');
--insert third and final set of records
INSERT INTO demo (text)
VALUES
('She bought a red bag'),
('Green is a good color'),
('The sky is blue'),
('The color of the shirt is red'),
('They plan to go with blue or red balloons');
--update statements that include the REPLACE function
UPDATE demo SET text = replace(text, 'Red', 'Yellow');
UPDATE demo SET text = replace(text, '-', '_');
UPDATE demo SET text = replace(text, 'red', 'yellow');
--view all the changes
SELECT * FROM demo;
In this article, we discussed how to replace a string using PostgreSQL replace()
and regexp_replace
the replace function, and how to replace()
update a string in a table using the replace function.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab