PostgreSQL CONCAT Function

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PostgreSQL CONCAT() function

CONCAT() function

The PostgreSQL concat() function is used to concatenate two or more strings except NULL specified in the arguments.

Syntax :

concat(<string1>,[<string2>,<string3>,...])

PostgreSQL Version : 9.3

Pictorial Presentation of PostgreSQL CONCAT() function<

Example : PostgreSQL CONCAT() function :

In the example below the strings specified in the argument have been concatenated and returns a string.

  1. SELECT concat('w',3,'r''esource','.','com');  

Output :

    concat
----------------
 w3resource.com
(1 row)

Example of PostgreSQL CONCAT() function with NULL :

In the example below, the concat function ignore the NULL and displays the result.

  1. SELECT concat('w',3,'r''esource',NULL,'.','com');  

Output :

    concat
----------------
 w3resource.com
(1 row)

Example of PostgreSQL CONCAT() function using column :

If we want to display the first_name, last_name, and Name of the employee for those employees who belongs to the department which ID is 100 from employees table the following statement can be executed.

  1. SELECT employee_id,first_name,last_name,  
  2. concat(first_name,' ',last_name) "Name of the Employee"   
  3. FROM employees  
  4. WHERE department_id=100;  

Output :

 employee_id | first_name  | last_name | Name of the Employee
-------------+-------------+-----------+----------------------
         108 | Nancy       | Greenberg | Nancy Greenberg
         109 | Daniel      | Faviet    | Daniel Faviet
         110 | John        | Chen      | John Chen
         111 | Ismael      | Sciarra   | Ismael Sciarra
         112 | Jose Manuel | Urman     | Jose Manuel Urman
         113 | Luis        | Popp      | Luis Popp
(6 rows)