Mysql Concat Function

MySQL: CONCAT Function

This tutorial explains how to use the MySQL CONCAT function with syntax and examples.

Description

The MySQL CONCAT function allows you to concatenate two or more expressions together.

Syntax

The syntax for the CONCAT function in MySQL is:

CONCAT( expression1, expression2, ... expression_n )

Parameters or Arguments

expression1, expression2, ... expression_n

The expressions to concatenate together.

Note

  • If expression is a numeric value, it will be converted by the CONCAT function to a binary string.
  • If all expressions are nonbinary strings, the CONCAT function will return a nonbinary string.
  • If any of the expressions is a binary string, the CONCAT function will return a binary string.
  • If any of the expressions is a NULL, the CONCAT function will return a NULL value.
  • See also the CONCAT_WS function.

Applies To

The CONCAT function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let's look at some MySQL CONCAT function examples and explore how to use the CONCAT function in MySQL.

For example:

mysql> SELECT CONCAT('The answer is ', 24);
Output: 'The answer is 24'

mysql> SELECT CONCAT('The answer is ', 10+10);
Output: 'The answer is 20'

mysql> SELECT CONCAT('AODBA.com', NULL);
Output: NULL

You can also concatenate expressions together in MySQL by placing the strings next to each other.

For example:

mysql> SELECT 'AO' 'DBA' '.com';
Output: 'AODBA.com'

mysql> SELECT 'The answer is ' '24';
Output: 'The answer is 24'

mysql> SELECT 'The answer is ' '10+10';
Output: 'The answer is 10+10'

Using the above method of concatenation, each of the expressions must be a string.