PostgreSQL – CONCAT_WS Function
Last Updated :
05 Nov, 2024
In PostgreSQL, the CONCAT_WS function is a powerful and versatile tool for concatenating strings with a specified separator. This function not only combines multiple string values but also efficiently handles NULL values, ignoring them in the concatenation process.
In this article, we will go deep into the syntax, practical examples, and outputs of the CONCAT_WS function, ensuring that we have a thorough understanding of how to utilize it effectively in our PostgreSQL queries.
What is the CONCAT_WS Function?
The CONCAT_WS function stands for “Concatenate With Separator.” It allows us to join multiple strings into a single string while inserting a specified separator between them. This is particularly useful when we want to create formatted strings or display data in a more readable format.
Syntax
CONCAT_WS(separator, string_1, string_2, ...);
Key Terms
- The separator is a string that is used to separate the resultant strings.
- The string_1, string_2, are strings that is to be converted.
- The CONCAT_WS function returns a combined string that is the combination of string_1, string_2, etc., separated by the separator.
Example 1: Concatenating Symbols
The following statement concatenates the symbols (ie, ^,+,-,/). The use of CONCAT_WS ensures that each part is clearly separated by the specified separator while ignoring any NULL values.
Query:
SELECT CONCAT_WS ('^^^', '+++ ', '---', '///');
Output

Explanation:
In this query, the function concatenates the strings ‘+++ ‘, ‘—‘, and ‘///‘ with the separator ‘^^^‘. The result is a single string where each of the original strings is separated by ‘^^^‘.
Example 2: Concatenating Multiple Strings
In this example, we will concatenate multiple strings, including text, to illustrate the flexibility of the CONCAT_WS function. Showing how CONCAT_WS not only combines the strings but also maintains clear separation between them with the specified separator. This example illustrates the function’s ability to handle various types of string data efficiently.
Query:
SELECT
CONCAT_WS ('***', 'geeks ', 'for', 'geeks');
Output

PostgreSQL CONCAT_WS Function Example2
Explanation:
Here, the function takes three strings—’Geeks ‘, ‘for‘, and ‘Geeks‘—and concatenates them using ‘***’ as the separator. The resulting string clearly illustrates how the CONCAT_WS function allows for customized formatting.
Example 3: Handling NULL Values
One of the notable features of the CONCAT_WS function is its ability to handle NULL values gracefully. Let’s demonstrate this with an example:
Query:
SELECT CONCAT_WS(', ', 'Hello', NULL, 'World', NULL, '!');
Output
Hello, World, !
Explanation:
In this query, the function concatenates ‘Hello‘, NULL, ‘World‘, NULL, and ‘!’ using a comma and space as the separator. The NULL values are ignored, resulting in a clean output string.
Conclusion
The CONCAT_WS function in PostgreSQL is an invaluable tool for developers and database administrators alike, providing an efficient way to concatenate strings with customized separators. By understanding its syntax and functionality, we can effectively format your output data for improved readability and presentation. Whether we’re working with text strings, symbols, or managing NULL values, CONCAT_WS simplifies string manipulation in our PostgreSQL queries.
Similar Reads
SQL Interview Questions
Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970s, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ 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
SQL Tutorial
SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases. In this
11 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 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
8 min read
ACID Properties in DBMS
In the world of Database Management Systems (DBMS), transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliabilit
8 min read
Introduction of DBMS (Database Management System)
A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 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
CTE in SQL
In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
SQL Triggers
SQL triggers are essential in database management systems (DBMS). They enable SQL statements to run when specific database events occur such as when someone adds, changes, or removes data. Triggers are commonly used to maintain data integrity, track changes, and apply business rules automatically, w
8 min read