PostgreSQL || operator
The PostgreSQL || operator can be used to concatenate two or more strings together. It can also be used to concatenate non-string values as well.
Syntax
string1 || string2 [ || string_n ]
Parameters
string1 |
Required. Specify the first string to concatenate. It must be a string value. |
string2 |
Required. Specify the second string to concatenate. It can be a string or non-string value. |
string_n |
Optional. Specify the nth string to concatenate. It can be a string or non-string value. |
Return Value
Returns the concatenated string.
Example 1:
The example below shows how to use || operator to concatenate two or more string and non-string values.
SELECT 'Alpha' || 'Coding' || 'Skills'; Result: 'AlphaCodingSkills' SELECT '10' || '20' || '30'; Result: '102030' SELECT '10' || 20 || '30'; Result: '102030' SELECT '10' || 20 || 30; Result: '102030' SELECT 'a' || 'b' || 'c' || 'd'; Result: 'abcd'
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 || operator is used to concatenate records of column FirstName and column LastName.
SELECT *, 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