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.)