EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL CONCAT()
 

PostgreSQL CONCAT()

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 6, 2023

PostgreSQL CONCAT()

 

 

Introduction to PostgreSQL CONCAT()

In order to concatenate more than two strings, we can use either the string concatenation operator (||) or CONCAT() function provided by PostgreSQL. The CONCAT() function is a built-in function provided by PostgreSQL since version 9.1. We can pass an array of string elements to the PostgreSQL CONCAT() function as it is a VARIADIC function and takes a list of elements as input. The PostgreSQL CONCAT() function ignores the NULL input if passed, which means we can concatenate the strings even if one of the input strings from a list of arguments provided is NULL.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Consider the following syntax of the CONCAT() function:

CONCAT(string_1, string_2, ...)

Explanation: The PostgreSQL CONCAT() function inputs an argument list.

How does PostgreSQL CONCAT() Function Work?

  • The PostgreSQL CONCAT() considers each list element as an argument, as this function is a VARIADIC function that can take any number of elements as input.
  • The input argument provided to the function should be string type or convertible to the string type.
  • The string type means the input argument should be of any of the following data types:
    • Varchar
    • Char
    • text

Examples

Consider the following example, which concatenates the two strings by using the PostgreSQL CONCAT() function. Illustrate the CONCAT function by using the following SELECT statement and a snapshot.

SELECT CONCAT ('Edu', 'CBA') as CONCAT_TABLE;

Output:

PostgreSQL CONCAT()-1.1

We will create a table named ‘student’ by using the CREATE TABLE statement as follows:

create table student
(
stud_id serial PRIMARY KEY,
stud_fname VARCHAR(80) NOT NULL,
stud_lname VARCHAR(80) NOT NULL
);

We will insert some data into the student table using the following INSERT INTO statement.

INSERT INTO student(stud_fname,stud_lname)
VALUES
('Smith','Johnson'),
('Williams','Jones'),
('Brown','Davis');

Illustrate the above INSERT statement’s result using the following SQL statement and snapshot.

select * from student;

Output:

PostgreSQL CONCAT()-1.2

Using the following SQL statement and snapshot, we will concatenate the values of the stud_fname and stud_lname columns of the student table.

SELECT
CONCAT  (stud_fname, ' ', stud_lname) AS "Students fullname"
FROM
Student;

Output:

PostgreSQL CONCAT()-1.3

We can concatenate the NULL values with strings using the CONCAT() function using the following SQL statement and snapshot.

SELECT CONCAT('EduCBA is',  NULL, ' awesome.') AS NULL_concatenated_string;

Output:

Output-1.4

We can see that the PostgreSQL CONCAT() function ignored the NULL string while joining the input arguments. At the same time, we will see the following SQL statement result, which uses the string concatenation operator (||) to concatenate the strings.

SELECT 'EduCBA is'|| NULL || 'awesome'  AS NULL_string;

Output:

Output-1.5

We can see the returned result is a NULL string, whereas the CONCAT function worked properly to ignore the NULL string.

We can use the PostgreSQL CONCAT() function for various operations like we need to build a statement that will give us the resultant string by combining the following strings:

  • ‘Student name is of.’
  • Length if the student’s first name.
  • ‘Characters.
SELECT
stud_fname,
CONCAT('Student name is of ', LENGTH (stud_fname), ' characters'   )
FROM
student;

Output:

Output-1.6

The PostgreSQL also provides the function named CONCAT_WS, which means concatenate with separator.

We will concatenate the stud_fname and stud_lname of the student table by using the following statement, which a single comma and space will separate.

SELECT
concat_ws (', ', stud_fname, stud_lname) AS student_full_name
FROM
student
ORDER BY
stud_fname;

Output:

Output-1.7

Advantages

  • We can concatenate two or more strings using the string concatenation operator (||), which fails with NULL arguments where the CONCAT() function ignores NULL arguments concatenate them.
  • The CONCAT() function is a variadic function that can take a list of the argument as input.
  • We can build the required output string by concatenating more than one strings.
  • The CONCAT() function operates on string type input only.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL CONCAT()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL JSON
  2. PostgreSQL Timestamp
  3. How to Work PostgreSQL HAVING?
  4. PostgreSQL RANDOM | Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW