MySQL String Concatenation with CONCAT()

Joining individual pieces of text data into a singular string is nothing new in the programming world and all languages support some form of concatenation. In MySQL, you can use the CONCAT() function. Let’s see how…

Image by Alejandro González from Pixabay 
Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


MySQL CONCAT() syntax

I’m using the ‘actor’ table from the well-known Sakila database for the example queries.

The MySQL CONCAT() function syntax is simple and easy to remember:

CONCAT(string_1,string_2, ... );

There should be at least 1 parameter passed into any CONCAT() function call or you will receive an error:

SELECT CONCAT() AS full_name
FROM actor
LIMIT 5;
Error Code: 1582. Incorrect parameter count in the call to native function 'CONCAT'

MySQL CONCAT() usage

As a novel attempt, we could try placing a space between the ‘first_name’ and ‘last_name’ columns from the ‘actor’ table, but, that doesn’t join the values into a single string:

SELECT first_name, ' ', last_name
FROM actor
LIMIT 5;

However, if you pass in both the ‘first_name’ and ‘last_name’ columns as parameters to CONCAT(), they are joined into a string:

SELECT CONCAT(first_name, last_name) AS full_name
FROM actor
LIMIT 5;

Discover premium articles, in-depth guides, instructional videos, and much more by joining the “MySQL Learning Tier” membership. You have access to exclusive content unpublished anywhere else with this membership. With new content being added regularly, continue learning MySQL at any level.


Notice there is no space between the ‘first_name’ and ‘last_name’ values in the previous example.

Yet, CONCAT() accepts multiple parameters, so you can simply pass in a space character as one of them:

SELECT CONCAT(first_name,' ', last_name) AS full_name
FROM actor
LIMIT 5;

In the below query, I’m passing 4 parameters to CONCAT(), 1 of which is prepended to the beginning of the string :

SELECT CONCAT('And the winner is: ', first_name,' ', last_name) AS full_name
FROM actor
LIMIT 5;

Try using CONCAT() in any of your queries where you need string concatenation.


Support my blog and content with a Tip today. Thank you for your time and donation 🙏


Similar reading

You may enjoy any of the below posts in addition to this one:

Thank you for reading this post. Please share it with someone else who would enjoy it as well.


Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.

More ways I can help

  • Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none.
  • Desktop and mobile wallpapers, digital downloads, photography services, Shopify and WooCommerce customizations, and content writing – all in one E-commerce Shop. Find your next digital purchase today!
  • Take your Laravel applications next level with Battle Ready Laravel by Ash Allen. Learn how to improve the performance, maintainability, and security of your Laravel projects in this e-book.

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.