MySQL CONCAT() Function

The CONCAT() function in MySQL combines two or more strings into a single string. It's a very useful function for creating combined text fields, labels, or other text-based outputs.



CONCAT(): Definition and Usage

CONCAT() provides a simple and efficient way to join strings. It's often used to create longer strings from individual parts, such as merging first and last names, or creating file paths from different directory and file names. If any of the input values are NULL, the result of the CONCAT() function will be NULL.

Syntax

Syntax

CONCAT(expression1, expression2, expression3, ...)
      

Parameter Values

Parameter Description
expression1, expression2, expression3, ... The strings or expressions to concatenate. At least one expression is required.

Examples

Concatenating String Literals

This example concatenates several string literals.

Syntax

SELECT CONCAT("SQL ", "Tutorial ", "is ", "fun!");
      
Output

SQL Tutorial is fun!
      

Concatenating Column Values

This example combines three columns ('Address', 'PostalCode', 'City') from the 'Customers' table into a single 'Address' column. (Assumes a 'Customers' table with those columns.)

Syntax

SELECT CONCAT(Address, " ", PostalCode, " ", City) AS Address
FROM Customers;
      
Output

Address
-------------------------------------------------
(The concatenated address for each customer will be shown here.)
      

**Note:** The second example's output will depend on the data in your `Customers` table. Each row will display the concatenated address string. If any of the `Address`, `PostalCode`, or `City` values are NULL for a given row, the entire result for that row will be `NULL`. For concatenating strings with a specific separator, see the `CONCAT_WS()` function.