MySQL String Concatenation with CONCAT()

Mysql

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 
The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

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;

Do you need to learn MySQL? Just starting and confused about how to query a table and get the data you need? I am creating premium MySQL Beginners content for this specific reason. To help those who want to learn the basics of MySQL but don’t know where to start. Learn more about the premium blog posts as I develop and release them.

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:

MySQL SHOW Tables – 2 variationsMySQL Beginners Series — The SELECT statementSUBSTRING_INDEX() function in MySQL – with examples.

Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.

Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

How can I help you?

Are you thinking of starting up a blog? I use WordPress for my blog. Let’s both save money on the plans offered. Grab a Gmail HTML Email Signature template from my Etsy shop and make your emails pop and stand out. Need hosting for your next web application or WordPress site? I use and highly recommend Hostinger. They have great pricing and service.I enjoy reading Refind: The essence of the web, every morning in your inbox. Subscribe for free. Help me get a premium subscription by signing up yourself with my referral link.Grab a free pack of mobile Creator wallpapers.Just getting started or wanting to learn MySQL? Find out about my premium blog posts and MySQL Beginner Series here.

Disclosure: Some of the services and products links in this post 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.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

The post MySQL String Concatenation with CONCAT() appeared first on Digital Owl’s Prose.