Name

CONCAT_WS( )

Synopsis

CONCAT_WS(separator, string,  . . . )

This function combines strings of text and columns together with a separator specified in the first argument. Any number of strings may be specified after the first argument, with each argument separated by a comma. Null values are ignored.

SELECT CONCAT_WS('|', student_id, name_last, name_first)
AS 'student_record'
FROM students LIMIT 3;
+-----------------------------+
| student_record              |
+-----------------------------+
| 433342000|Dyer|Russell      |
| 434892001|Dyer|Marie        |
| 433892002|Zabalaoui|Michael |
+-----------------------------+

Here the vertical bar is used to separate the columns. This function can be useful for exporting data to formats acceptable to other software. However, you will require some programming to export the data to an external file using an SQL statement such as this one.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.