MySQL CONCAT() Function
In MySQL, the CONCAT()
function is used to join two or more specified strings in order and return the result. If you need to join multiple strings with delimiters, please use the CONCAT_WS()
function.
The CONCAT()
function returns NULL
if one of the arguments is NULL
.
CONCAT()
Syntax
Here is the syntax of MySQL CONCAT()
function:
CONCAT(string1, string2, ..., stringN)
Parameters
string1, string2, ..., stringN
- Required. You must specify at least one string. If one of the parameters is
NULL
, MySQLCONCAT()
function will be returnedNULL
; If no string is specified, MySQLCONCAT()
function will report an error:ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CONCAT'
.
Return value
The CONCAT()
function returns the joined string.
- The
CONCAT()
function will returnNULL
if one of the arguments isNULL
. - If there is only one parameter, the
CONCAT()
function will return the parameter itself.
CONCAT()
Example
-
To join the strings
'Hello'
and'World'
, use the following statement:SELECT CONCAT('Hello', 'World');
+--------------------------+ | CONCAT('Hello', 'World') | +--------------------------+ | HelloWorld | +--------------------------+
Note that the
CONCAT()
function doesn’t add a delimiter to the concatenated string, so the result is'HelloWorld'
. If you want to separate with spaces, use the following statement:SELECT CONCAT('Hello', ' ', 'World');
+-------------------------------+ | CONCAT('Hello', ' ', 'World') | +-------------------------------+ | Hello World | +-------------------------------+
-
Returns
NULL
if one of the arguments isNULL
. See the example below:SELECT CONCAT('Hello', NULL);
+-----------------------+ | CONCAT('Hello', NULL) | +-----------------------+ | NULL | +-----------------------+