Concat_ws in mysql

Summary: in this tutorial, you will learn various ways to concatenate two or more strings together by using the MySQL CONCAT and CONCAT_WS functions.

To concatenate two or more quoted string values, you place the string next to each other as the following syntax:

SELECT 'MySQL ' 'String ' 'Concatenation';

Code language: SQL (Structured Query Language) (sql)

Try It Out

Concat_ws in mysql

MySQL string concatenation is cleaner in comparison with other database management systems. For example, if you use PostgreSQL or Oracle, you have to use the string concatenation operator ||. In Microsoft SQL server, you use the addition arithmetic operator (+) to concatenate string values.

Besides using spaces for string concatenation, MySQL provides two other functions that concatenate string values: CONCAT and CONCAT_WS.

The MySQL CONCAT function takes one or more string arguments and concatenates them into a single string. The CONCAT function requires a minimum of one parameter otherwise it raises an error.

The following illustrates the syntax of the CONCAT function.

CONCAT(string1,string2, ... );

Code language: SQL (Structured Query Language) (sql)

The CONCAT function converts all arguments to the string type before concatenating. If any argument is NULL, the CONCAT function returns a NULL value.

The following statement concatenates two quoted strings: MySQL and CONCAT.

SELECT CONCAT('MySQL','CONCAT');

Code language: SQL (Structured Query Language) (sql)

Try It Out

Concat_ws in mysql

If you add a NULL value, the CONCAT function returns a NULL value as follows:

SELECT CONCAT('MySQL',NULL,'CONCAT');

Code language: SQL (Structured Query Language) (sql)

Try It Out

Concat_ws in mysql

See the following customers table in the sample database.

Concat_ws in mysql

To get the full names of contacts, you use the CONCAT function to concatenate first name, space, last name as the following statement:

SELECT concat(contactFirstName,' ',contactLastName) Fullname FROM customers;

Code language: SQL (Structured Query Language) (sql)

Try It Out

Concat_ws in mysql

MySQL CONCAT_WS function: Concatenate strings with a separator

MySQL provides a special form of the CONCAT function: CONCAT_WS function. The CONCAT_WS function concatenates two or more string values with a predefined separator.

The following illustrates the syntax of the CONCAT_WS function:

CONCAT_WS(seperator,string1,string2, ... );

Code language: SQL (Structured Query Language) (sql)

The first argument is the separator for other arguments: string1, string2, …

The CONCAT_WS function adds the separator between string arguments and returns a single string with the separator inserted between string arguments.

The following statement concatenates two string values: John and Doe, and separates these two strings by a comma:

SELECT CONCAT_WS(',','John','Doe');

Code language: SQL (Structured Query Language) (sql)

Try It Out

Concat_ws in mysql

The CONCAT_WS function returns NULL if and only if the first argument, which is the separator, is NULL. See the following example:

SELECT CONCAT_WS(NULL ,'Jonathan', 'Smith');

Code language: SQL (Structured Query Language) (sql)

Try It Out

Concat_ws in mysql

Unlike the CONCAT function, the CONCAT_WS function skips NULL values after the separator argument. In other words, it ignores NULL values.

SELECT CONCAT_WS(',','Jonathan', 'Smith',NULL);

Code language: SQL (Structured Query Language) (sql)

Try It Out

Concat_ws in mysql

The following statement constructs complete addresses using the CONCAT_WS function:

SELECT CONCAT_WS(CHAR(13), CONCAT_WS(' ', contactLastname, contactFirstname), addressLine1, addressLine2, CONCAT_WS(' ', postalCode, city), country, CONCAT_WS(CHAR(13), '')) AS Customer_Address FROM customers;

Code language: SQL (Structured Query Language) (sql)

Try It Out

Here is the output result:

Customer_Address --------------------------------------------------- Schmitt Carine 54, rue Royale 44000 Nantes France King Jean 8489 Strong St. 83030 Las Vegas USA ...

Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use MySQL CONCAT and CONCAT_WS functions to concatenate one or more string value into a single string.

Was this tutorial helpful?

What does Concat_ws function do in MySQL?

The MySQL CONCAT_WS function allows you to concatenate two or more expressions together and adds a separator between each of the concatenated expressions.

What is the use of Concat_ws?

The CONCAT_WS() function adds two or more expressions together with a separator.

What is difference between concat and Concat_ws?

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.

How do I concatenate 3 columns in SQL?

SQL Server CONCAT() Function.
Add two strings together: SELECT CONCAT('W3Schools', '.com');.
Add 3 strings together: SELECT CONCAT('SQL', ' is', ' fun!' );.
Add strings together (separate each string with a space character): SELECT CONCAT('SQL', ' ', 'is', ' ', 'fun!' );.