Sling Academy
Home/PostgreSQL/PostgreSQL: How to use concatenation operator (||) to combine strings

PostgreSQL: How to use concatenation operator (||) to combine strings

Last updated: January 04, 2024

Introduction

String concatenation is a fundamental operation in any database system, and PostgreSQL uses the double pipe || as a concatenation operator to merge strings together efficiently. This tutorial explores the versatile applications of the || operator in PostgreSQL.

Basic Concatenation

To begin with, let’s look at the simplest form of string concatenation. Given two strings, ‘Hello’ and ‘World’, you can combine them into a single string, ‘HelloWorld’, using the || operator:

SELECT 'Hello' || 'World';

You will receive the combined string as the result:

HelloWorld

Concatenating with Spaces

More often, you’ll want a space between words. You can include the space as part of your string literal:

SELECT 'Hello' || ' ' || 'World';

The result would be:

Hello World

Using Concatenation with Columns

It’s common to use the || operator to combine columns in a result set. If your database has a table ‘users’ with ‘first_name’ and ‘last_name’ columns, you can create a full name by concatenating the two:

SELECT first_name || ' ' || last_name AS full_name FROM users;

Handling NULL Values

Concatenation can become tricky when NULL values are involved, as any string combined with NULL using the || operator will result in NULL. Use the COALESCE function to handle NULL values gracefully:

SELECT first_name || ' ' || COALESCE(last_name, '') AS full_name FROM users;

This ensures that if last_name is null, an empty string will take its place.

Concatenating Numbers and Strings

Strings can be concatenated with numbers implicitly without the need for casting:

SELECT 'Your order number is ' || 25 || '.' AS order_confirmation;

In this case, PostgreSQL will automatically convert the integer 25 to a string before concatenation.

Concatenating with Aggregate Functions

PostgreSQL allows you to concatenate strings across rows using the STRING_AGG function:

SELECT STRING_AGG(name || '(', ')') FROM products;

This concatenates product names and adds parentheses around them.

Advanced Concatenation Scenarios

You can also use the || operator to build dynamic SQL queries within a plpgsql function, or to concatenate text within array processing.

Creating Conditions with Concatenation

Dynamic SQL in PostgreSQL can benefit greatly from the || operator:

CREATE OR REPLACE FUNCTION dynamic_query(search_term TEXT) RETURNS void AS $$
BEGIN
  EXECUTE 'SELECT * FROM products WHERE name LIKE ''%' || search_term || '%'';';
END;
$$ LANGUAGE plpgsql;

In this function, the search term is concatenated into a dynamic query within the function’s EXECUTE statement.

Concatenating Arrays

Strings can also be concatenated within arrays. To concatenate a prefix with every element of an array:

SELECT ARRAY(SELECT 'Item: ' || s FROM unnest(ARRAY['one', 'two', 'three']) AS s);

This code produces an array of strings prefixed with ‘Item: ‘.

Conclusion

The || operator in PostgreSQL is a powerful tool for combining strings. From simple concatenations to its use in dynamic SQL and array operations, understanding || expands your ability to manipulate and display data in custom formats. Test your knowledge with the given examples and look for ways to integrate string concatenation in your PostgreSQL queries!

Next Article: PostgreSQL: Using CONCAT and CONCAT_WS Functions to Combine Strings

Previous Article: Using ‘IS NULL’ and ‘IS NOT NULL’ in PostgreSQL

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB