MySQL

MySQL CONCAT()

The CONCAT() function in MySQL, allows you to concatenate two or more strings. The function allows one or more arguments, but its main use is to concatenate two or more strings.
 

Syntax:
CONCAT(expression1, expression2, ...)

 

 

Example:

Let’s take an example of using CONCAT function to see how it works. For this we will use the “Persons” table.

> SELECT * FROM Persons;

+----------+-----------+--------+-------------------------------+
|    ID    |    Name   |  age   |             Address           |
+----------+-----------+--------+-------------------------------+
|    101   |    Alex   |   25   | 819 Saint Francis Way         |
|    102   |   Emily   |   15   | 171 Jarvisville Road Michigan |
|    103   |   Jean    |   35   | 188 Clay Street Indiana       |
|    104   |    Bob    |   40   | 285 Java Lane Missouri        |
+----------+-----------+--------+-------------------------------+

In the query below, we will add the three columns “ID”, “Name” and “Age” separated with dashes “-” in a single column named “Result”:

SELECT CONCAT(ID, "-", Name, "-", Age) AS Result FROM Persons;

Output:

+----------------+
|     Result     |
+----------------+
| 101-Alex-25    |
| 102-Emily-15   |
| 103-Jean-35    |
| 104-Bob-40     |
+----------------+
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *