CONCAT function in SQL
In SQL, the CONCAT function is used to combine two or more strings into a single string.
Example:
SQL
CREATE TABLE Employees ( EmployeeID INT, FirstName VARCHAR(20), LastName VARCHAR(20), Age int );
Insert the data in the Employees table.
SQL
insert into Employees values (1, "Ashish", “Goel", 25), (2, "Kirshan", "Gupta", 30), (3, "Anjali", "Sharma", 25), (4, "Manju", "Saini", 15), (5, "Katrina", "Kaif", 18), (6, "Esha", "Dixit", 20), (7, "Ankita", "Verma", 19), (8, "Meenakshi", "Chikkara", 35), (10, "Alia", "Bhatt", 16);
By using the Create table and insert statement we reach to the following table in our database:
Table: Employees
EmployeeID | First Name | Last Name | Age |
---|---|---|---|
1 | Ashish | Goel | 25 |
2 | Kirshan | Gupta | 30 |
3 | Anjali | Sharma | 25 |
4 | Manju | Saini | 15 |
5 | Katrina | Kaif | 18 |
6 | Esha | Dixit | 20 |
7 | Ankita | Verma | 19 |
8 | Meenakshi | Chikkara | 35 |
10 | Alia | Bhatt | 16 |
Use the CONCAT() function to concatenate the firstname and last name.
SQL
SELECT EmployeeID, FirstName, LastName, CONCAT(firstname, “ ”, lastname) AS FullName FROM Employees;
The output of the above query is shown below:
EmployeeID | First Name | Last Name | Full Name |
---|---|---|---|
1 | Ashish | Goel | Ashish Goel |
2 | Kirshan | Gupta | Kirshan Gupta |
3 | Anjali | Sharma | Anjali Sharma |
4 | Manju | Saini | Manju Saini |
5 | Katrina | Kaif | Katrina Kaif |
6 | Esha | Dixit | Esha Dixit |
7 | Ankita | Verma | Ankita Verma |
8 | Meenakshi | Chikkara | Meenakshi Chikkara |
10 | Alia | Bhatt | Alia Bhatt |