MySQL Concatenate

The MySQL concatenate function returns the string that results from concatenating (joining) the arguments (input strings) which are passed to it.

There are many reasons one might want to concatenate two strings. You may want to do something as simple as joining a first name and a last name.

Whatever your reason is, have a look at the MySQL concatenate tutorial below. It looks at concatenating strings in MySQL.

MySQL Concatenate Syntax

The syntax for the MySQL concatenate function is as follows:

CONCAT(string1, string2, ...);

In other words, the concat function in MySQL can have one or more arguments and it will join them all before returning the result.

MySQL Concatenate Example

Look at the following examples of using MySQL to perform a concatenation and the results they give:

SELECT CONCAT('I love ', 'Tutorial ', 'Arena');

The result:

'I love Tutorial Arena'

A more practical example is to use data from a table in the database to perform our concat on. Suppose we had the following table (called "employees") in our database:

first_name last_name
Paul Pitterson
Francine Beecham
Raul DiNozzo
Anthony DiNozzo

We could actually join the first name and last name of the employees using the following command:

SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM employees;

The result would be:

Paul Pitterson
Francine Beecham
Raul DiNozzo
Anthony DiNozzo

As you can see, we instructed MySQL to concatenate the data in the first_name column with the data in the last_name column. We also had to explicitly insert a space between the first name and last name so they didn't come out joined after the concatenation.

We even renamed the column in our result to "fullname" which is a more meaningful name than the "CONCAT(first_name, ' ', last_name)" that we would have gotten otherwise.

We hope this MySQL concatenation tutorial was helpful.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...