PostgreSQL CONCAT() Function
The PostgreSQL CONCAT() function is used to concatenate two or more expressions together. This function may have one or more arguments. If any of the expressions is a NULL, this function ignores NULL values during concatenation.
Syntax
CONCAT(expr1, expr2, ... expr_n)
Parameters
expr1, expr2, ... expr_n |
Required. Specify the expressions to concatenate together. |
Return Value
Returns the concatenated string.
Example 1:
The example below shows the usage of CONCAT() function.
SELECT CONCAT('SQL ', 'Tutorial'); Result: 'SQL Tutorial' SELECT CONCAT('Learning ', 'SQL ', 'is ', 'fun!.'); Result: 'Learning SQL is fun!.' SELECT CONCAT('Sum is ', 25 + 25); Result: 'Sum is 50' SELECT CONCAT('Alpha ', 'Beta', ' Gamma'); Result: 'Alpha Beta Gamma' SELECT CONCAT('Alpha ', 'Beta', NULL, ' Gamma'); Result: 'Alpha Beta Gamma'
Example 2:
Consider a database table called Employee with the following records:
EmpID | FirstName | LastName |
---|---|---|
1 | John | Smith |
2 | Marry | Knight |
3 | Jo | Williams |
4 | Kim | Fischer |
5 | Ramesh | Gupta |
6 | Huang | Zhang |
In the query below, the CONCAT() function is used to concatenate records of column FirstName and column LastName.
SELECT *, CONCAT(FirstName, ' ', LastName) AS FullName FROM Employee;
This will produce the result as shown below:
EmpID | FirstName | LastName | FullName |
---|---|---|---|
1 | John | Smith | John Smith |
2 | Marry | Knight | Marry Knight |
3 | Jo | Williams | Jo Williams |
4 | Kim | Fischer | Kim Fischer |
5 | Ramesh | Gupta | Ramesh Gupta |
6 | Huang | Zhang | Huang Zhang |
❮ PostgreSQL Functions