How to Concat Two Columns Into One in MySQL?
Last Updated : 07 Aug, 2024
Concatenating columns in MySQL is a key operation for combining data from multiple fields into a single, unified column. This process is essential for generating comprehensive reports and merging data elements like names or addresses.
This article explores two effective methods for column concatenation using the CONCAT()
function and the REPLACE()
function.
How to Concatenate Two Columns Using MySQL?
Concatenating columns is a common requirement in data manipulation and reporting. We might need to combine first names and last names into a full name or merge address components into a single address field.MySQL provides multiple approaches to achieve this, each with its use cases and benefits.
- Using the
CONCAT
Function - Using the REPLACE Function
Let’s set up an Environemnt
Demo Table
CREATE DATABASE geeks;
USE geeks;
CREATE TABLE demo_table(
FIRSTNAME VARCHAR(20),
LASTNAME VARCHAR(20),
AGE INT);
INSERT INTO demo_table VALUES
('Romy', 'Kumari', 21),
('Pushkar', 'Jha', 22),
('Meenakshi', 'Jha', 19),
('Rinkle', 'Arora', 22),
('Ayushi', 'Choudhary', 21),
('Sujata', 'Jha', 31);
Output:

There are two methods to concat columns in MySQL, depending on your use case, you can use any of these techniques to concat two columns.
Method 1: Using the CONCAT
Function
This method will not make changes to the original table.
For the demonstration, we will concatenate FIRSTNAME and LASTNAME and will name the column FIRSTNAME.
Query:
SELECT *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME
FROM demo_table;
Output:

Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(‘ ‘) in CONCAT() function.
Query:
SELECT *, CONCAT(FIRSTNAME,' ', LASTNAME) AS FIRSTNAME
FROM demo_table;
Output:

Method 2: Using the REPLACE Function
This method will change the original table.
For the demonstration, we will replace FIRSTNAME with the concatenated value of FIRSTNAME and LASTNAME column. To do this, we can use REPLACE function with CONCAT function.
Query:
UPDATE demo_table
SET FIRSTNAME = REPLACE(FIRSTNAME,FIRSTNAME, CONCAT(FIRSTNAME,' ', LASTNAME));
Output:
Conclusion
MySQL offers flexible methods for concatenating columns, with CONCAT()
being ideal for temporary views and reports, while REPLACE()
is useful for permanent data updates. Understanding these techniques allows you to efficiently manage and manipulate your data according to your specific needs.
Similar Reads
How to Convert Rows into Columns in MySQL?
Converting rows into columns, also known as pivoting or transposing, is a common operation in DBMS, and MySQL provides robust functionality for achieving this transformation. This process is useful to reshape data for better analysis or reporting. This guide will explore the syntax, usage, and examp
3 min read
How to Combine Two Columns into One in R dataframe?
In this article, we will discuss how to combine two columns into one in dataframe in R Programming Language. Method 1 : Using paste() function This function is used to join the two columns in the dataframe with a separator. Syntax: paste(data$column1, data$column2, sep=" ") where data is the input
2 min read
How to Get Column Names in MySQL?
To get column names in MySQL use techniques such as the DESCRIBE statement, INFORMATION_SCHEMA.COLUMNS, and SHOW COLUMNS FROM commands. Here will cover these techniques, with explained examples, and help to get a better understanding on how to get column names in MySQL. MySQL Fetch Column Names from
3 min read
How to Get the Type of Columns in SQL
In SQL, the types of columns in a database table play a fundamental role in data management and query execution. Each column is designed to store specific types of data, such as numbers, text, dates, or binary data. Understanding these column types is essential for effective database design, query o
4 min read
How to Efficiently Convert Rows to Columns in SQL?
In SQL, rows and columns are the fundamental building blocks of a database. Rows represent individual records, while columns represent the attributes or characteristics of those records. However, there may be instances where we need to convert rows to columns in order to better analyze and manipulat
5 min read
How to Rename a Column in MySQL?
Renaming columns in MySQL is a frequent task to keep data organized and flexible. It helps adjust database layouts to fit new needs without losing information. This article will show you different ways to rename columns in MySQL, making it easier to manage and update your database structure as your
4 min read
How to Efficiently Convert Rows to Columns in PostgreSQL?
Converting rows to columns, often referred to as pivoting or transposing, is a crucial aspect of data transformation in SQL. This technique is useful for improving data readability, facilitating analysis, aligning data formats with the requirements of reporting tools, and optimizing queries. In Post
5 min read
How to Convert BLOB into VARCHAR in MySQL?
In this article, we would be learning a SQL query to convert a column of BLOB Data Type to VARCHAR Data Type. To execute this query we would need to alter the table and subsequently a column's definition. We would first need to use the ALTER TABLE command to change the table. ALTER TABLE: ALTER TABL
2 min read
How to Get the Datatype of Table Columns in MySQL?
When working with MySQL databases, knowing the datatype of table columns is essential to maintain data integrity and avoid errors. This guide covers methods to check column datatypes, such as using SHOW COLUMNS and querying INFORMATION_SCHEMA.COLUMNS. Understanding column datatypes helps in designin
4 min read
How to Check a Column is Empty or Null in MySQL?
In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the
4 min read