PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Advanced
    • Indexes
    • Views
    • Triggers
    • Administration
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
    • Window Functions
Home / PostgreSQL String Functions / PostgreSQL CONCAT Function

PostgreSQL CONCAT Function

Summary: in this tutorial, we will show you how to use the PostgreSQL CONCAT and CONCAT_WS functions to concatenate two or more strings into one.

Introduction to PostgreSQL CONCAT function

To concatenate two or more strings into one, you use the string concatenation operator || as the following example:

1
2
SELECT
   'Concatenation' || ' ' || 'Operator' AS result_string;

PostgreSQL Concatenation Operator

The following statement concatenates a string with a NULL value:

1
2
SELECT
   'Concat with ' || NULL AS result_string;

It returns a NULL value.

Since version 9.1, PostgreSQL introduced a built-in string function named CONCAT to concatenate two or more strings into one.

The following illustrates the syntax of the CONCAT function:

1
CONCAT(str_1, str_2, ...)

The CONCAT function accepts a list of arguments. The argument needs to be convertible to a string. A string in this context means any of the following data types: char, varchar, or text.

The CONCAT function is variadic. It means that the CONCAT function accepts an array as the argument. In this case, you need to mark the array with the VARIADIC keyword. The CONCAT function treats each array element as an argument.

If you want to know more about VARIADIC, check it out the function parameters tutorial for the detailed information.

Unlike the concatenation operator ||, the CONCAT function ignores  NULL arguments. We will see it in detail in the following section.

PostgreSQL CONCAT function examples

The following SELECT statement uses the CONCAT function to concatenate two strings into one:

1
2
SELECT
   CONCAT ('CONCAT',' ', 'function');

PostgreSQL Concat function example

The following statement concatenates values in the first_name and last_name columns of the customer table in the sample database.

1
2
3
4
SELECT
   CONCAT  (first_name, ' ', last_name) AS "Full name"
FROM
   customer;

Postgres CONCAT function example

In the following example, we use CONCAT function to concatenate a string with a NULL value.

1
2
SELECT
   CONCAT('Concat with ',NULL) AS result_string;

PostgreSQL concat function with a NULL value

As you see, unlike the string concatenation operator ||, the CONCAT function ignores the NULL arguments.

The following statement concatenates strings with a number returned from the LENGTH function.

1
2
3
4
5
6
7
8
9
SELECT
   first_name,
   concat (
      'Your first name has ',
      LENGTH (first_name),
      ' characters'
   )
FROM
   customer;

postgresql concat integer to string

PostgreSQL CONCAT_WS function

Besides the CONCAT function, PostgreSQL also provides you with the CONCAT_WS function that concatenates strings into one separated by a particular separator. By the way, WS stands for with separator.

Like the CONCAT function, the CONCAT_WS function is also variadic and ignored NULL values.

The following illustrates the syntax of the CONCAT_WS function.

1
CONCAT_WS(separator,str_1,str_2,...);

The separator is a string that separates all arguments in the result string.

The str_1, str_2, etc., are strings or any arguments that can be converted into strings.

The CONCAT_WS function returns a combined string that is the combination of str_1, str_2, etc., separated by the separator.

PostgreSQL CONCAT_WS function example

The following statement concatenates the last name and first name and separates them by a comma and a space:

1
2
3
4
5
6
SELECT
   concat_ws (', ', last_name, first_name) AS full_name
FROM
   customer
ORDER BY
   last_name;

postgresql concat_ws function example

In this tutorial, we have shown you how to use the PostgreSQL CONCAT and CONCAT_WS function to concatenates two or more strings into one.

  • Was this tutorial helpful ?
  • YesNo
Previous Tutorial: PostgreSQL LPAD Function
Next Tutorial: PostgreSQL TRANSLATE Function

PostgreSQL String Functions

  • ASCII
  • CONCAT
  • CHR
  • FORMAT
  • LEFT
  • LENGTH
  • LPAD
  • MD5
  • POSITION
  • REGEXP_MATCHES
  • REGEXP_REPLACE
  • RIGHT
  • REPLACE
  • SPLIT_PART
  • SUBSTRING
  • TRANSLATE
  • TRIM
  • TO_CHAR
  • TO_NUMBER

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL DROP TRIGGER
  • PostgreSQL NTILE Function
  • PostgreSQL PERCENT_RANK Function
  • PostgreSQL CUME_DIST Function
  • PostgreSQL Sequences
  • PostgreSQL LAG Function
  • PostgreSQL LEAD Function
  • PostgreSQL NTH_VALUE Function
  • PostgreSQL LAST_VALUE Function
  • PostgreSQL FIRST_VALUE Function

Site Info

  • Home
  • PostgreSQL Cheat Sheet
  • Resources
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2019 by PostgreSQL Tutorial Website. All Rights Reserved.

⤒