MySQL: CONCAT WS Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL CONTACT WS Function

In this guide, we will explain 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_nThe expressions to concatenate together.

Note

  • The CONCAT_WS function skips expressions (ie: expression1expression2, … 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('_', 'ad', 'gl', 'o', 'b');
Result: 'ad_gl_o_b'

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

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

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

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

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

Next Topic : Click Here

This Post Has One Comment

Leave a Reply