MySQL MariaDB

How Do I Use the CONCAT() Function in MySQL?

When it comes to working with MySQL, there are many useful functions available to help you manipulate and manage your data. The CONCAT() function is one of those functions that allow you to combine two or more strings into a single string. This function can be incredibly useful when it comes to working with large datasets for management and analysis.

This guide will provide detailed information on how to use the CONCAT() function in MySQL.

How Do I Utilize the CONCAT() Function in MySQL?

The “CONCAT()” function is utilized to merge two or more strings into one string, by taking multiple strings as arguments and returning a single concatenated string value. The syntax to join strings using CONCAT() function is given below:

CONCAT('string1', 'string2', ...)

 
In the above syntax, specify the string values of your choice in place of string1, string2, etc.

Let’s see a few examples of the CONCAT() function to understand their working.

Example 1: Concat Two Strings

Type the given below command to join two strings using the “CONCAT()” function and retrieve the result using the “SELECT” statement:

SELECT CONCAT('Linux', 'Hint');

 
In the above example, you can change the strings “Linux”, and “Hint” with the strings that you want to merge.

Output


The output showed that the “Linux”, and “Hint” strings have been joined to a single string as “LinuxHint”.

Example 2: Concat More Than Two Strings

To merge more than two strings you just need to increase the number of arguments in the CONCAT() function as given below:

SELECT CONCAT('Linux', ' ', 'Hint', '!');

 
In the above example, the second argument is a single space, and the fourth one is a special character means special characters will also be considered as a string and can be joined.

Output


The output displayed that the strings have been concatenated.

Example 3: Join Column Values

The “CONCAT()” function can be utilized with the SELECT statement to join the specific column values of a particular table. The example of concatenating two columns “FIRST_NAME” and “LAST_NAME” of the “CUSTOMERS” table is provided below:

SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) AS FULL_NAME FROM CUSTOMERS;

 
In the above example, the output will be displayed as “FULL_NAME”.

Output


The output showed that the specified column values have been joined.

Example 4: Join String and Column Values

The CONCAT() function can be utilized to join the column values of a table with a specific string. The example of joining the “WELCOME” string and the “FIRST_NAME” and “LAST_NAME” columns of the “CUSTOMERS” table is given below:

SELECT CONCAT('WELCOME, ', FIRST_NAME, ' ', LAST_NAME)
AS GREETING FROM CUSTOMERS;

 
Output


The output showed that the string and column values have been concatenated.

Example 5: Join Column Values From Different Tables

The “CONCAT()” function can join the column values of two different tables and give the result as a single value, as shown in the example given below:

SELECT CONCAT(ORDERS.PRODUCT_ID ,' ', PRODUCTS.NAME ,' (', ORDERS.QUANTITY ,')')
AS DETAILS
FROM ORDERS, PRODUCTS
WHERE ORDERS.PRODUCT_ID = PRODUCTS.ID;

 
In the above example, the “PRODUCT_ID” and “QUANTITY” columns of the “ORDERS” tables and the “NAME” column of the “PRODUCTS” table are concatenated based on the condition that is used within the “WHERE” clause.

Output


The output showed that the values have been joined to a single value.

Example 6: Join The Column Values Using IFNULL()

In MySQL, the “IFNULL()” is a function that allows the user to check if the value is NULL or not. The “CONCAT()” function can be utilized with the “IFNULL()” function to check (if the value is NULL or not) and join the column values of a particular table.

Here is the example of joining the column value using IFNULL():

SELECT CONCAT(IFNULL(FIRST_NAME, ''), ' ', IFNULL(LAST_NAME, ''))
AS FULL_NAME FROM CUSTOMERS;

 
In the above example, IFNULL() function returns the non-null values of the “FIRST_NAME” and the “LAST_NAME” columns of the “CUSTOMERS” table and then the “CONCAT()” function concatenated the returned values.

Output


The output showed that the “FIRST_NAME” and the “LAST_NAME” column of the “CUSTOMERS” table have been merged based on the result of the IFNULL() function.

Example 7: Join the Column Values Using CONCAT_WS()

In the “CONCAT_WS()” function, the “WS” stands for “with separator” which means the “CONCAT_WS()” is utilized to concatenate two or more strings together with a specified separator.

The example of the CONCAT_WS() function is provided below:

SELECT CONCAT_WS(', ', ADDRESS, CITY, STATE) AS LOCATION FROM CUSTOMERS;

 
In the above example, the “ADDRESS”, “CITY”, and “STATE” columns of the “CUSTOMERS” table are used with the “,” separator.

Output


The output displayed that the specified columns have been joined with the comma “,” separator.

Conclusion

The “CONCAT()” function in MySQL is a powerful tool that allows you to join strings and column values in various ways, making it easier to manipulate and analyze your data. By following the examples discussed above, you can start using this function in your own MySQL queries to combine text and data in new and meaningful ways. This write-up explained various use cases of the CONCAT() function in MySQL.

About the author

Danish Ghafoor

I am a computer science graduate with a certification in cybersecurity. My passion for technology has led me to develop a deep understanding of the challenges and solutions in the field of information security. I am dedicated to staying up-to-date with the latest industry trends and technologies, and constantly seeking out new opportunities to expand my knowledge and skills.