MySQL CONCAT_WS() Function

In MySQL, the CONCAT_WS() function is used to join two or more specified strings with the specified delimiter, and return the joined string.

The CONCAT_WS() function returns NULL if one of the arguments is NULL.

CONCAT_WS() Syntax

Here is the syntax of MySQL CONCAT_WS() function:

CONCAT_WS(separator, string1, string2, ..., stringN)

Parameter Description

Required. The separator. You can use a single character or a string.
string1, string2, ..., stringN
Required. If a parameter is NULL, it will be ignored; If no string is specified, MySQL CONCAT() function will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CONCAT'.

Return value

The CONCAT_WS() function returns the joined strings.

  • If separator is NULL, the CONCAT_WS() function will return NULL.
  • If there is a NULL value in string1, string2, ..., stringN, it will be ignored by CONCAT_WS().

CONCAT_WS() Examples

  • To join 'Hello' and 'World' with -, use the following statement:

    SELECT CONCAT_WS('-', 'Hello', 'World');
    | CONCAT_WS('-', 'Hello', 'World') |
    | Hello-World                      |
  • The delimiter can use a string:

    SELECT CONCAT_WS('----', 'Hello', NULL, 'World');
    | CONCAT_WS('----', 'Hello', NULL, 'World') |
    | Hello----World                            |
  • The CONCAT_WS() function ignores the NULL value parameter. See the example below:

    SELECT CONCAT_WS('-', 'Hello', NULL, 'World');
    | CONCAT_WS('-', 'Hello', NULL, 'World') |
    | Hello-World                            |