PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the REPLACE()
function is used to replace all occurrences of a substring with another substring within a string.
REPLACE(source_string, search_string, replacement_string)
Where:
source_string
is the original string.search_string
is the substring that you want to find and replace.replacement_string
is the substring that will replace the search_string
.The function returns a new string with all occurrences of the search_string
replaced by the replacement_string
.
Let's look at an example to understand the usage of the REPLACE()
function.
SELECT REPLACE('PostgreSQL is great', 'great', 'awesome');
This would return:
PostgreSQL is awesome
If search_string
is not found in source_string
, the REPLACE()
function returns the original string unchanged.
If search_string
is an empty string or NULL, the REPLACE()
function returns the original string.
If replacement_string
is an empty string, the REPLACE()
function will remove all occurrences of search_string
from the source_string
.
Using the REPLACE()
function, you can easily modify strings in PostgreSQL by replacing specific substrings with desired values.
How to use REPLACE function in PostgreSQL:
The REPLACE
function is used to replace occurrences of a specified substring with another substring in a string.
SELECT REPLACE('original_string', 'old_substring', 'new_substring') AS replaced_string;
Replacing substrings with REPLACE in PostgreSQL:
SELECT REPLACE('Hello World', 'World', 'Universe') AS replaced_string; -- Output: Hello Universe
Case-sensitive and case-insensitive replacements with REPLACE in PostgreSQL:
-- Case-sensitive replacement SELECT REPLACE('Hello World', 'world', 'Universe') AS replaced_string; -- Output: Hello World -- Case-insensitive replacement SELECT REPLACE(LOWER('Hello World'), 'world', 'Universe') AS replaced_string; -- Output: Hello Universe
Replacing multiple occurrences with REPLACE in PostgreSQL:
SELECT REPLACE('one two one two one', 'one', 'three') AS replaced_string; -- Output: three two three two three
Handling NULL values with REPLACE in PostgreSQL:
SELECT REPLACE(NULL::text, 'old_value', 'new_value') AS replaced_string; -- Output: NULL
Using REPLACE with other string functions in PostgreSQL:
SELECT REPLACE(UPPER('Hello World'), 'WORLD', 'Universe') AS replaced_string; -- Output: HELLO Universe
Combining REPLACE with regular expressions in PostgreSQL:
SELECT REPLACE(REGEXP_REPLACE('Hello123', '\d', ''), 'Hello', '') AS replaced_string; -- Output: 123
Replacing values in specific columns with REPLACE in PostgreSQL:
UPDATE your_table SET your_column = REPLACE(your_column, 'old_value', 'new_value');
Using REPLACE in UPDATE statements in PostgreSQL:
UPDATE your_table SET your_column = REPLACE(your_column, 'old_value', 'new_value') WHERE your_condition;
Replacing characters or patterns in specific positions with REPLACE in PostgreSQL:
SELECT REPLACE(SUBSTRING('Hello World' FROM 1 FOR 5), 'Hello', 'Hi') || SUBSTRING('Hello World' FROM 6) AS replaced_string; -- Output: Hi World
Using REPLACE with different data types in PostgreSQL:
SELECT REPLACE(123::text, '2', '5') AS replaced_string; -- Output: 153
Replacing values in text and varchar columns with REPLACE in PostgreSQL:
SELECT REPLACE(your_text_column, 'old_value', 'new_value') AS replaced_string FROM your_table;
Optimizing queries with REPLACE in PostgreSQL:
Use REPLACE
judiciously, as it can impact performance for large datasets. Ensure indexes are used efficiently.
REPLACE function with JOIN operations in PostgreSQL:
SELECT t1.id, REPLACE(t1.name, t2.old_value, t2.new_value) AS replaced_name FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id;
Handling special characters with REPLACE in PostgreSQL:
SELECT REPLACE('Special$Character', '$', '') AS replaced_string; -- Output: SpecialCharacter
Using REPLACE for data cleansing in PostgreSQL:
UPDATE your_table SET your_column = REPLACE(your_column, 'unwanted_pattern', '');