Concatenate strings and fields in MySQL


Home - Tutorials - 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:17109

Bookmark Concatenate strings and fields in MySQL



AddThis Social Bookmark Button

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:

Code:
  1. CONCAT(str1,str2,...)

A very basic example to concatenate two static strings is this:

Code:
  1. SELECT CONCAT('Hello ','World!');

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:

Code:
  1. SELECT CONCAT(name, ' is ', age,' years old.') FROM USER;

And the output is something like this:

Code:
  1. Bill is 28 years old.                                                                                                    
  2. Mark is 32 years old.                                                                                                    
  3. Julia is 44 years old.                                                                                                   
  4. 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:

Code:
  1. Bill is 28 years old.                                                                                                    
  2. Mark is 32 years old.                                                                                                    
  3. Julia is 44 years old.                                                                                                   
  4. Alex is 52 years old.                                                                                                    
  5. NULL                                                                                                                         
  6. NULL                                                                                                                         






Tags: mysql concat, concatenate strings, concatenate fields, combine strings

Concatenate strings and fields in MySQL - Table of contents
Step 1 - MySQL Concat function



F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1

Family tutorials
PHP Array
PHP Redirect
PHP Session
JavaScript String
JavaScript Timeout
JavaScript Tooltip
AJAX PHP
AJAX File Upload
AJAX Rating System

Total time: 0.0302