CONCAT

Used to concatenate two strings.

Syntax

CONCAT ('string1','string2')

Behavior Type

Immutable

Parameters

'string1'

Can be any datatype.

'string2'

Restrictions

Varbinary and long varbinary types cannot be mixed with other types. These types return varbinary and long varbinary, respectively.

Similarly, long varchar types return long varchar.

Otherwise, the result is varchar.

If either argument is null, concat returns null.

Example

The following simple examples use a sample table named alphabet, which contains two rows, letter1 and letter2. The contents are as follows.

=> CREATE TABLE alphabet (letter1 varchar(2), letter2 varchar(2));
CREATE TABLE
=> COPY alphabet FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> A|B
>> C|D
>> \.
=> SELECT * FROM alphabet;
 letter1 | letter2
---------+---------
 C       | D
 A       | B
(2 rows)

The following example concatenates the contents of the first column with a character string.

=> SELECT CONCAT(letter1, ' is a letter') FROM alphabet;
    CONCAT
---------------
 A is a letter
 C is a letter
(2 rows)

The following example nests the CONCAT function.

=> SELECT CONCAT(CONCAT(letter1, ' and '), CONCAT(letter2, ' are both letters')) FROM alphabet;
          CONCAT
--------------------------
 C and D are both letters
 A and B are both letters
(2 rows)