It’s often useful to concatenate two fields together in an SQL query. In PostgreSQL, fields may be concatenated using the || operator. The syntax is really simple. Just place the double bar between the fields you want to join, and (optionally) give a label to that new field. Strings can also be concatenated with the fields.

For example, let’s say you have a person table with first_name and surname fields, and you wanted to return the full name of each person. This could be done with the following:

SELECT first_name || ' ' || surname AS name
FROM person

This will give you a result set similar to:

     name
---------------
 Bob Dylan
 Stevie Wonder
 Bob Marley
 Elvis Presley
(4 rows)

Note that this syntax will only work with PostgreSQL – other database servers may use different syntax.

  1. Frank says:

    The usage of double pipes is standard SQL and works in many databases, including Firebird, MySQL (SQL_mode ANSI_SQL) and Oracle. Nothing special and has nothing to do with PostgreSQL. PostgreSQL just uses the standard, that’s it.