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

PostgreSQL - REPLACE Function

In PostgreSQL, the REPLACE() function is used to replace all occurrences of a substring with another substring within a string.

Syntax:

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.

Returns:

The function returns a new string with all occurrences of the search_string replaced by the replacement_string.

Example:

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

Notes:

  • 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.

  1. 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;
    
  2. Replacing substrings with REPLACE in PostgreSQL:

    SELECT REPLACE('Hello World', 'World', 'Universe') AS replaced_string;
    -- Output: Hello Universe
    
  3. 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
    
  4. 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
    
  5. Handling NULL values with REPLACE in PostgreSQL:

    SELECT REPLACE(NULL::text, 'old_value', 'new_value') AS replaced_string;
    -- Output: NULL
    
  6. Using REPLACE with other string functions in PostgreSQL:

    SELECT REPLACE(UPPER('Hello World'), 'WORLD', 'Universe') AS replaced_string;
    -- Output: HELLO Universe
    
  7. Combining REPLACE with regular expressions in PostgreSQL:

    SELECT REPLACE(REGEXP_REPLACE('Hello123', '\d', ''), 'Hello', '') AS replaced_string;
    -- Output: 123
    
  8. Replacing values in specific columns with REPLACE in PostgreSQL:

    UPDATE your_table
    SET your_column = REPLACE(your_column, 'old_value', 'new_value');
    
  9. Using REPLACE in UPDATE statements in PostgreSQL:

    UPDATE your_table
    SET your_column = REPLACE(your_column, 'old_value', 'new_value')
    WHERE your_condition;
    
  10. 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
    
  11. Using REPLACE with different data types in PostgreSQL:

    SELECT REPLACE(123::text, '2', '5') AS replaced_string;
    -- Output: 153
    
  12. 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;
    
  13. Optimizing queries with REPLACE in PostgreSQL: Use REPLACE judiciously, as it can impact performance for large datasets. Ensure indexes are used efficiently.

  14. 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;
    
  15. Handling special characters with REPLACE in PostgreSQL:

    SELECT REPLACE('Special$Character', '$', '') AS replaced_string;
    -- Output: SpecialCharacter
    
  16. Using REPLACE for data cleansing in PostgreSQL:

    UPDATE your_table
    SET your_column = REPLACE(your_column, 'unwanted_pattern', '');