A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql-concat-function/ below:

PostgreSQL- CONCAT Function - GeeksforGeeks

PostgreSQL- CONCAT Function

Last Updated : 15 Jul, 2025

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

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', 'john@gmail.com', '123-456-7890'),
('Jane Smith', 'jane.smith@gmail.com', NULL);

SELECT CONCAT(name, ' (', email, ') ', phone) AS contact_info


FROM contacts;

Output

contact_info
-----------------------------
John Doe (john@gmail.com) 123-456-7890
Jane Smith (jane.smith@gmail.com)

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



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4