The MySQL Concat_ws string function stands for concatenating with a separator. The 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 function is as shown below:
SELECT CONCAT_WS (Separator, String 1, String 2,.., String N) FROM Source
MySQL Concat_ws String Function 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.
Concat_ws Example 2
In this MySQL example, we are going to concatenate the FirstName and LastName columns present in the Employe using a space separator, and dollar separator. To demonstrate this CONCAT_WS function, we are going to use the employ table data that we have shown below.
Let me show you the query. Remember, this function is the same as the Concat function. However, this Function accepts an extra argument as the separator.
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;