Skip to content

CONCAT, ||

Concatenate a series of strings.

CONCAT(string [, string [ , … ] ])

You can also use the || operator to concatenate a series of strings. CONCAT and || also accept values that are not character strings, such as integers and timestamps.

The CONCAT function and the || operator have exactly the same behavior.

Trailing spaces in CHAR strings are ignored (trimmed) when they are concatenated. Trailing spaces in VARCHAR strings are preserved.

Examples

The following example concatenates four strings (two VARCHAR columns (name and nickname) and two literal values).

premdb=# SELECT CONCAT(name,', The ',nickname) AS names 
FROM team LIMIT 5;
            names              
--------------------------------
 Arsenal, The Gunners
 Aston Villa, The Villains
 Birmingham City, The Blues
 Blackpool, The Seasiders
 Bolton Wanderers, The Trotters
(5 rows)

The following example concatenates three character columns:

premdb=# SELECT name||nickname||city AS names FROM team LIMIT 5;
            names              
--------------------------------
 ArsenalGunnersLondon
 Aston VillaVillainsBirmingham
 Birmingham CityBluesBirmingham
 BlackpoolSeasidersBlackpool
 Bolton WanderersTrottersBolton
(5 rows)

The following example concatenates two SMALLINT columns and a colon character:

premdb=# select concat(htid,':',atid) from team order by htid;
 concat 
--------
 2:51
 3:52
 4:53
 5:54
 6:55
 7:56
...

Compare the results of concatenated strings with trailing spaces in CHAR and VARCHAR expressions:

premdb=> select 'abc '::char(5) || 'zzz '::char(5) as concat_char, length(concat_char) from sys.const;
 concat_char | length 
-------------+--------
 abczzz      |      6
(1 row)

premdb=> select 'abc '::varchar(5) || 'zzz '::varchar(5) as concat_varchar, length(concat_varchar) from sys.const;
 concat_varchar | length 
----------------+--------
 abc zzz        |      8
(1 row)