Mysql Concat_ws Function

MySQL: CONCAT_WS Function

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

Description

The MySQL CONCAT_WS function allows you to concatenate two or more expressions together and adds a separator between each of the concatenated expressions.

Syntax

The syntax for the CONCAT_WS function in MySQL is:

CONCAT_WS( separator, expression1, expression2, ... expression_n )

Parameters or Arguments

separator

The separator that is added between each of the concatenated expressions.

expression1, expression2, ... expression_n

The expressions to concatenate together.

Note

  • The CONCAT_WS function skips expressions (ie: expression1, expression2, ... expression_n) that contain a NULL value.
  • If separator is a NULL, the CONCAT_WS function will return a NULL value.
  • See also the CONCAT function.

Applies To

The CONCAT_WS 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_WS function examples and explore how to use the CONCAT_WS function in MySQL.

For example:

mysql> SELECT CONCAT_WS(',', 1, 2, 3, 4);
Output: '1,2,3,4'

mysql> SELECT CONCAT_WS(', ', 1, 2, 3, 4);
Output: '1, 2, 3, 4'

mysql> SELECT CONCAT_WS('ABC', 'x', 'y', 'z');
Output: 'xABCyABCz'

mysql> SELECT CONCAT_WS('ABC', 'x', 'y', NULL, 'z');
Output: 'xABCyABCz'

mysql> SELECT CONCAT_WS(NULL, 'x', 'y', 'z');
Output: NULL