Concatenate strings and fields in MySQL
This article demonstrates how to concatenate two or more strings and fields in MySQL using the CONCAT() function.
Tutorial info:
| Name: | Concatenate strings and fields in MySQL |
| Total steps: | 1 |
| Category: | MySQL |
| Date: | 2010-11-10 |
| Level: | Beginner |
| Product: | See complete product |
| Viewed: | 12292 |
Bookmark Concatenate strings and fields in MySQL
Step 1 - MySQL Concat function
Concatenate strings and fields in MySQL
In cases when you want to combine two or more strings in MySQL you can use the CONCAT() function.
The syntax is the following:
A very basic example to concatenate two static strings is this:
Of course you can use table fields instead of string constants and you can mix them as well. Let’s see a bit more complicated example. Suppose we have a user table with name and age and you want to output the age of every user in a human readable sentence. The SELECT statement uses the CONCAT function in the following way:
And the output is something like this:
Bill is 28 years old.
Mark is 32 years old.
Julia is 44 years old.
Alex is 52 years old.
Please note that if one of the field is NULL then the complete result also will be NULL. In case of the previous example if there would be a record where the age field is NULL and a record where the name field is NULL then the output would be:
Bill is 28 years old.
Mark is 32 years old.
Julia is 44 years old.
Alex is 52 years old.
NULL
NULL
Tags: mysql concat, concatenate strings, concatenate fields, combine strings
| Concatenate strings and fields in MySQL - Table of contents |
|---|
| Step 1 - MySQL Concat function |