How Do You Concatenate in DB2 SQL?

Use the CONCAT function to concatenate together two strings or fields using the syntax CONCAT(expression1, expression2). Though concatenation can also be performed using the || (double pipe) shortcut notation, errors are thrown if DB2 is not configured to allow it.

  1. Open an SQL command line, and type the CONCAT command

    Open an SQL command line. In the command line type CONCAT. Follow it immediately with an open parenthesis.

  2. Supply the first expression

    Input the first expression. This can be either a string or a field from a table in the database. Follow the first expression with a comma.

  3. Supply the second expression

    After the comma, supply the second expression. As with the first expression, this can be either a string or a field from a table in the database. Follow the second expression with a closed parenthesis.

  4. Execute the command

    Hit the Entry key to execute the command. If both expressions are strings, as with “CONCAT(‘Hello’, ‘World’)”, the output of the command is a single string concatenation of the two, “HelloWorld.” If both expressions are fields, as with “SELECT CONCAT(City, State) FROM Locations”, the output is a set with all the entries in the first column joined with all the entries in the second column. If one expression is a string and the other a field, as with “SELECT CONCAT(City, ‘NY’) FROM NYCities”, the output is a set with the string expression either prepended or appended to each entry in the given column.

ADVERTISEMENT