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
PostgreSQL- CONCAT Function
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 SQ
4 min read
PostgreSQL - FORMAT Function
The PostgreSQL format() function is a powerful tool for string formatting by allowing developers to insert variables into strings using format specifiers like %s, %I, and %L. This function is especially useful for building dynamic SQL queries and ensuring proper formatting of identifiers. It simplif
3 min read
PostgreSQL - CHR Function
The CHR() function in PostgreSQL is used to convert an integer ASCII code or a Unicode code point into its corresponding character. Let us better understand the concept of CHR Function in PostgreSQL from this article. SyntaxCHR(value);Parameter:value: The 'value' argument is generally an integer ASC
2 min read
PostgreSQL - DROP FUNCTION
In PostgreSQL, the DROP FUNCTION statement is essential for removing functions from your database. Let us learn more about the syntax and the detailed examples to ensure you understand how to use this statement effectively. SyntaxDROP FUNCTION [IF EXISTS] function_name(argument_list) [CASCADE | REST
4 min read
PostgreSQL - STRING_AGG() Function
The STRING_AGG() function in PostgreSQL is a powerful aggregate function used to concatenate a list of strings with a specified separator. This function is essential for combining string values from multiple rows into a single string, making data aggregation more efficient and readable. Let us get a
3 min read
PostgreSQL - ASCII() Function
When working with PostgreSQL, you might need to derive the ASCII (American Standard Code for Information Interchange) code of a character. The PostgreSQL ASCII() function is a handy tool for this purpose. In the case of UTF-8 encoding, the ASCII() function returns the Unicode code point of the chara
2 min read
PostgreSQL String Functions
PostgreSQL is a powerful, open-source relational database management system that offers a rich set of functions and operators for working with string data. String manipulation is an essential task in many applications, and PostgreSQL provides a variety of built-in functions to make working with text
8 min read
MySQL | Group_CONCAT() Function
The GROUP_CONCAT() function in MySQL is an aggregation function that combines data from multiple rows into a single string. It is particularly useful for aggregating summaries, such as combining related information into a single field for better readability or reporting. In this article, we will exp
4 min read
PostgreSQL REVERSE() Function
The REVERSE() function in PostgreSQL is a simple yet powerful tool used to reverse the order of characters in a given string. It takes one input which is a string and returns the characters in reverse order. This function is helpful when you need to transform data, run tests or validate information.
4 min read
PostgreSQL - TRIM Function
The TRIM() function in PostgreSQL is an essential tool for removing unwanted characters from strings. Whether we're working with user inputs, formatting text, or performing data cleansing operations, TRIM() is an invaluable function for managing string data. This article will provide an in-depth loo
4 min read