PostgreSQL- CONCAT Function
Last Updated :
11 Nov, 2024
The PostgreSQL CONCAT function allows us to combine multiple strings or column values into a single output, making it a flexible tool for data manipulation. This function is essential for string concatenation tasks, whether we’re working with static text, columns from a database table, or dynamic SQL queries.
In PostgreSQL, we have the CONCAT function and the concatenation operator (||
), each with unique advantages. In this guide, we will explain how to use the PostgreSQL CONCAT function, understand its syntax, and cover practical examples using tables in a sample database for clarity.
What is the PostgreSQL CONCAT Function?
The PostgreSQL CONCAT function concatenates (combines) two or more strings into a single string, handling NULL values gracefully by ignoring them. Introduced in PostgreSQL 9.1, CONCAT provides flexibility in string concatenation by accepting multiple arguments of string types like CHAR, VARCHAR, and TEXT.
Syntax
CONCAT(string_1, string_2, ...)
Key Terms
- String Convertible Arguments: The CONCAT function accepts a variable number of arguments, each convertible to a string (CHAR, VARCHAR, or TEXT).
- Variadic Functionality: Since CONCAT is variadic, it can take a list of strings as arguments or accept an array using the VARIADIC keyword.
- NULL Handling: Unlike the
||
operator, CONCAT ignores NULL values in the concatenation process, which prevents NULL results in the final string.
PostgreSQL CONCAT Function Examples
Let us take a look at some of the examples of the CONCAT Function in PostgreSQL to better understand the concept. We will use a dvdrental sample database to showcase real-world applications.
Example 1: Concatenating Multiple Strings
The CONCAT function efficiently combines the strings in the order provided, without any delimiters, making it ideal for straightforward string concatenation tasks. The below statement uses the CONCAT function to concatenate three strings into one
Query:
SELECT CONCAT ('Geeks', 'for', 'geeks');
Output

Explanation:
The result of this query will be a single string: '
GeeksforGeeks
'
.
Example 2: Concatenating Columns with Static Text
The following statement concatenates values in the 'first_name' and 'last_name' columns of the actor table in the sample database, ie, dvdrental.
Query:
SELECT CONCAT (first_name, ' ', last_name) AS "Full name"
FROM actor;
Output

Explanation:
Here, the CONCAT function merges each actor's first and last names with a space in between, listing them alphabetically.
Example 3: Using CONCAT with NULL Values
This example demonstrates how NULL values are handled. We’ll use a contacts table and concatenate the columns name, email, and phone.
Query:
CREATE TABLE contacts (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255),
phone VARCHAR(15)
);
INSERT INTO contacts (name, email, phone)
VALUES
('John Doe', '[email protected]', '123-456-7890'),
('Jane Smith', '[email protected]', NULL);
SELECT CONCAT(name, ' (', email, ') ', phone) AS contact_info
FROM contacts;
Output
contact_info
-----------------------------
John Doe ([email protected]) 123-456-7890
Jane Smith ([email protected])
Explanation:
NULL values (e.g., missing phone numbers) are ignored, making the CONCAT function useful for avoiding unexpected NULL results in concatenated strings.
Important Points About PostgreSQL CONCAT Function
- The
CONCAT
function is variadic, meaning it can take a variable number of arguments, including arrays.
- Unlike the concatenation operator (
||
), the CONCAT
function ignores NULL
arguments, which prevents unexpected NULL
results in your concatenated strings.
- The
CONCAT
function can be easily combined with other PostgreSQL functions like UPPER()
, LOWER()
, TRIM()
, and more to perform complex string manipulations.
- The
CONCAT
function is supported in PostgreSQL 9.1 and later.
Conclusion
In conclusion, the CONCAT() function in PostgreSQL provides a flexible solution for string concatenation, allowing us to efficiently combine multiple text values or columns into a single output. By understanding the syntax of the CONCAT() function and its usage with examples, we can handle various data formatting tasks with ease. Whether we use CONCAT() or the concatenation operator (||), PostgreSQL offers flexible options to meet our requirements.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15+ min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read