MySQL Concat_ws string

The MySQL Concat_ws string function stands for concatenating with separator. MySQL concat_ws function is one of the String methods, which is to combine two or more strings along with a separator. If any of the arguments is a Null value, then it returns the result as the. NULL

The basic syntax of the Concat_ws in MySQL is as shown below:

SELECT CONCAT_WS (Separator, String 1, String 2,.., String N)
FROM Source

MySQL Concat_ws String Example

It returns the combined string using the separator. The following query shows you the same.

SELECT CONCAT_WS(',', 'Tutorial', 'Gateway') AS Str1;

SELECT CONCAT_WS(' ', 'Tutorial', 'Gateway') AS Str2;

SELECT CONCAT_WS(' $ ', 'Learn', 'MySQL', 'at', 'Tutorial', 'Gateway') AS Str3;

SELECT CONCAT_WS(' ', 'Learn', 'MySQL', 'at', 'Tutorial', 'Gateway') AS Str4;

As you can see from the below screenshot, it is combining the words using the separator that we specified as the first argument.

MySQL Concat_ws string 1

Concat_ws Example 2

In this MySQL example, we are going to concatenate the FirstName and LastName columns present in the Employe using space separator, and dollar separator. To demonstrate this method, we are going to use the employ table data that we have shown below

MySQL Concat_ws string 2

Let me show you the query

SELECT  First_Name, 
        Last_Name,
	CONCAT_WS(' ', First_Name, Last_Name) AS FullName1,
        CONCAT_WS(' $ ', First_Name, Last_Name) AS FullName2,
	DepartmentName,
        Email
FROM employe;
MySQL Concat_ws string 3

TIP: This function is the same as the Concat function. However, this Function accepts extra argument as the separator.