What is the difference between CONCAT() and CONCAT_WS() functions?

MySQLMySQLi Database

Both CONCAT() and CONCAT_WS() functions are used to concatenate two or more strings but the basic difference between them is that CONCAT_WS() function can do the concatenation along with a separator between strings, whereas in CONCAT() function there is no concept of the separator. Other significance difference between them is that CONCAT()function returns NULL if any of the argument is NULL, whereas CONCAT_WS() function returns NULL if the separator is NULL.

Example

The example below demonstrate the difference between CONCAT() and CONCAT_WS() function −

mysql> Select CONCAT('Ram','is','a','good','student') AS 'Example of CONCAT()';

+---------------------+
| Example of CONCAT() |
+---------------------+
| Ramisagoodstudent   |
+---------------------+

1 row in set (0.00 sec)

mysql> Select CONCAT_WS(' ','Ram','is','a','good','student') AS 'Example of CONCAT_WS()';

+------------------------+
| Example of CONCAT_WS() |
+------------------------+
| Ram is a good student  |
+------------------------+

1 row in set (0.00 sec)
raja
Published on 20-Feb-2018 10:19:00
Advertisements