What is the use of MySQL CONCAT_WS() function?


Basically, MySQL CONCAT_WS() function is used to concatenate two or more strings along with a separator. Here the keyword WS in CONCAT_WS() means WITH SEPARATOR. We can pronounce CONCAT_WS() function as concatenation function with a separator.

Syntax

CONCAT_WS(Separator, String1,String2,…,StringN)

Here, the arguments of CONCAT_WS functions are Separator and the strings which need to be concatenated along with that separator as a single string. Separator except for numeric value must enclose within quotes.

Example

mysql> SELECT CONCAT_WS(' ','New', 'Delhi');

+-------------------------------+
| CONCAT_WS(' ','New', 'Delhi') |
+-------------------------------+
| New Delhi                     |
+-------------------------------+

1 row in set (0.00 sec)

In this example above, we can see that the string ‘ ‘ (i.e. a white space) works as a separator and inserted between two strings, New and Delhi) which needs to be concatenated.

mysql> SELECT CONCAT_WS(' is our ','Delhi','Capital');

+-----------------------------------------+
| CONCAT_WS(' is our ','Delhi','Capital') |
+-----------------------------------------+
| Delhi is our Capital                    |
+-----------------------------------------+

1 row in set (0.00 sec)

In this example above, we can see that the string ‘ is our ‘ works as a separator and inserted between two strings, Delhi and Capital, which needs to be concatenated.

Updated on: 30-Jul-2019

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements