MySQL Concat_ws string

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

MySQL Concat_ws String Syntax

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

-- MySQL Concat_WS String Syntax 
SELECT CONCAT_WS (Separator, String 1, String 2,.., String N)
FROM Source

MySQL Concat_ws String Example 1

The MySQL Concat_ws String Function returns the combined string using the separator. The following Concat_ws query shows you the same.

-- MySQL String Concat_ws Example
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 string using the separator that we specified as the first argument.

MySQL Concat_ws string 1

MySQL Concat_ws String 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 concat_ws string function, 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

-- MySQL String Concat_ws Example
USE `mysql tutorial`;
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: The Concat_ws function is the same as the Concat function. However, this String Function accepts extra argument as the string separator.