MySQL – Introduction to CONCAT and CONCAT_WS functions

MySQL supports two types of concatenation functions. They are CONCAT and CONCAT_WS

MySQL - Introduction to CONCAT and CONCAT_WS functions

CONCAT function just concats all the argument values as such

SELECT CONCAT('Television','Mobile','Furniture');

The above code returns the following

TelevisionMobileFurniture

If you want to concatenate them with a comma, either you need to specify the comma at the end of each value, or pass comma as an argument along with the values

SELECT CONCAT('Television,','Mobile,','Furniture');
SELECT CONCAT('Television',',','Mobile',',','Furniture');

Both the above return the following

Television,Mobile,Furniture

However you can omit the extra work by using CONCAT_WS function. It stands for Concatenate with separator. This is very similar to CONCAT function, but accepts separator as the first argument.

SELECT CONCAT_WS(',','Television','Mobile','Furniture');

The result is

Television,Mobile,Furniture

If you want pipeline as a separator, you can use

SELECT CONCAT_WS('|','Television','Mobile','Furniture');

The result is

Television|Mobile|Furniture

So CONCAT_WS is very flexible in concatenating values along with separate.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

MySQL
Previous Post
SQL SERVER – What are Actions in SSAS and How to Make a Reporting Action
Next Post
Column Aliases: Where You Can Use Them and Where You Cannot

Related Posts

1 Comment. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.