The CONCAT_WS() function combines two or more expressions into one string, with a separator between each expression. WS stands for “With Separator”.
MySQL CONCAT_WS Function Syntax
SQL
x
1
CONCAT_WS(separator, expression1, expression2, expression3, ...);
MySQL CONCAT_WS Function Example
This query returns the combined string ‘SQL-Tutorial-is-fun!’ with – as the separator.
SQL
1
1
SELECT CONCAT_WS('-', 'SQL', 'Tutorial', 'is', 'fun!') AS ConcatenatedString;
Note: The CONCAT_WS() function is supported in MySQL but not in SQLite. This example does not work in the TechKubo playground.
MySQL CONCAT_WS Function Parameters
Parameter | Description |
---|---|
separator | Required. The separator to place between each expression. If NULL , the result is NULL . |
expression1, expression2, … | Required. The expressions to concatenate. Expressions with NULL values are skipped. |
MySQL CONCAT_WS Function Table Column Example
This query returns the full patient info by combining first_name, last_name, and gender, separated by spaces.
SQL
1
1
SELECT CONCAT_WS(' ', first_name, last_name, gender) AS PatientInfo
2
FROM Patients;
Note: The CONCAT_WS() function is supported in MySQL but not in SQLite. This example does not work in the TechKubo playground. You can use || and manually add separators if needed.
MySQL CONCAT_WS Function Notes
- Combines multiple expressions with a specified separator.
- If any expression is
NULL
, it is skipped (separator is not added forNULL
values).- If the separator is
NULL
, the result isNULL
.- Works in MySQL 4.0 and higher.
- Not supported in SQLite. In SQLite, use
||
for string concatenation and insert separators manually.