Introduction to PostgreSQL CONCAT()
In order to concatenate more than two strings we can use either string concatenation operator (||) or CONCAT() function provided by the 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.
Consider the following syntax of the CONCAT() function:
CONCAT(string_1, string_2, ...)
Explanation: The PostgreSQL CONCAT() function takes an argument list as an input.
How does PostgreSQL CONCAT() Function Work?
- The PostgreSQL CONCAT() considers each element of a list as an argument as this function is a VARIADIC function which can take any number of elements as input.
- The input argument provided to the function should be of string type or can be convertible to the string type.
- The string type means the input argument should be of any of the following data types:
Examples to Implement PostgreSQL CONCAT() Function
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;
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
Now, we will insert some data into the student table by using the INSERT INTO statement as follows
INSERT INTO student(stud_fname,stud_lname)
Illustrate the result of the above INSERT statement by using the following SQL statement and snapshot.
select * from student;
We will concatenate the values of the stud_fname and stud_lname columns of the student table by using the following SQL statement and snapshot.
CONCAT (stud_fname, ' ', stud_lname) AS "Students fullname"
We can concatenate the NULL values with strings by using the CONCAT() function by using the following SQL statement and snapshot.
SELECT CONCAT('EduCBA is', NULL, ' awesome.') AS NULL_concatenated_string;
We can see that the PostgreSQL CONCAT() function ignored the NULL string while joining the input arguments. Whereas we will see the result of the following SQL statement which uses string concatenation operator (||) to concatenate the strings.
SELECT 'EduCBA is'|| NULL || 'awesome' AS NULL_string;
We can see the returned result is a NULL string whereas CONCAT function worked properly to ignore NULL string.
We can use the PostgreSQL CONCAT() function for various operations like we need to build a statement which will give us the resultant string by combining the following strings:
- ‘Student name is of ‘
- Length if the student’s first name.
CONCAT('Student name is of ', LENGTH (stud_fname), ' characters' )
The PostgreSQL also provides us with 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 will be separated by the single comma and space.
concat_ws (', ', stud_fname, stud_lname) AS student_full_name
- As we can concatenate the two or more string by using the string concatenation operator (||) which fails with NULL arguments where the CONCAT() function ignores NULL arguments and concatenate them.
- The CONCAT() function is a variadic function which means it 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.
This is a guide to PostgreSQL CONCAT(). Here we also discuss the definition and how does PostgreSQL concat() function work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –