CONCAT() function in MySQL
Last Updated :
07 Jun, 2022
CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. If a numeric argument is given then it is converted to its equivalent nonbinary string form. Syntax :
CONCAT(str1, str2, ...)
Parameter : This method accepts N argument.
- str1, str2.str3.... : The input string which we want to concatenate.
Returns : It returns a new string after concatenating all input string. If any of the input string is NULL then it returns NULL. Example-1 : Concatenating 3 string using CONCAT Function.
SELECT CONCAT('geeks', 'for', 'geeks') AS ConcatenatedString ;
Output :
ConcatenatedString |
---|
geeksforgeeks |
Example-2 : Concatenating numeric string using CONCAT Function.
SELECT CONCAT(19, 10, 5.60) AS ConcatenatedNumber ;
Output :
ConcatenatedNumber |
---|
19105.60 |
Example-3 : Concatenating string which includes a NULL String using CONCAT Function.
SELECT CONCAT('geeks', 'for', 'geeks', NULL) AS ConcatenatedString ;
Output :
Example-4 : In this example we are going to concatenate string between column of a table. To demonstrate create a table named Student.
CREATE TABLE Student(
StudentId INT AUTO_INCREMENT,
FirstName VARCHAR(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Class VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
State VARCHAR(20) NOT NULL,
PinNo INT NOT NULL,
PRIMARY KEY(StudentId )
);
Now inserting some data to the Student table :
INSERT INTO
Student(FirstName, LastName, Class, City, State, PinNo )
VALUES
('Sayantan', 'Maity', 'X', 'Kolkata', 'WestBengal', 700001 ),
('Nitin', 'Shah', 'XI', 'Jalpaiguri', 'WestBengal', 735102 ),
('Aniket', 'Sharma', 'XI', 'Midnapore', 'WestBengal', 721211 ),
('Abdur', 'Ali', 'X', 'Malda', 'WestBengal', 732101 ),
('Sanjoy', 'Sharama', 'X', 'Kolkata', 'WestBengal', 700004 ) ;
So, the Student table is :
Select * From Student ;
StudentId | FirstName | LastName | Class | City | State | PinNo |
---|
1 | Sayantan | Maity | X | Kolkata | WestBengal | 700001 |
2 | Nitin | Shah | XI | Jalpaiguri | WestBengal | 735102 |
3 | Aniket | Sharma | XI | Midnapore | WestBengal | 721211 |
4 | Abdur | Ali | X | Malda | WestBengal | 732101 |
5 | Sanjoy | Sharama | X | Kolkata | WestBengal | 700004 |
Now, we will concatenate FirstName and LastName to get FullName and City, State and PinNo to get Address using CONCAT Function.
Select
StudentId, FirstName, LastName,
CONCAT(FirstName, ' ', LastName) AS FullName,
CONCAT(City, ' ', State, ' ', PinNO) AS Address
FROM Student;
Output :
StudentId | FirstName | LastName | FullName | Address |
---|
1 | Sayantan | Maity | Sayantan Maity | Kolkata WestBengal 700001 |
2 | Nitin | Shah | Nitin Shah | Jalpaiguri WestBengal 735102 |
3 | Aniket | Sharma | Aniket Sharma | Midnapore WestBengal 721211 |
4 | Abdur | Ali | Abdur Ali | Malda WestBengal 732101 |
5 | Sanjoy | Sharama | Sanjoy Sharama | Kolkata WestBengal 700004 |
Similar Reads
COUNT() Function in MySQL The COUNT() function in MySQL is a versatile aggregate function used to determine the number of rows or non-NULL values that match a specific condition in a query. It can be applied to an entire table or a particular column and is widely used in database operations to analyze the volume of data in a
3 min read
COT() Function in MySQL COT() function : This function in MySQL is used to return the cotangent of a specified number. If the specified number is 0, an error or NULL will be returned. In a right triangle, the cotangent of an angle is the length of it's adjacent side divided by the length of the opposite side. Similarly, th
1 min read
CURDATE() Function in MySQL The CURDATE() function in MYSQL is used to return the current date. The date is returned to the format of "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). This function equals the CURRENT_DATE() function. In this article, we are going to discuss about CURDATE() function in detail. Syntax CURDATE(); P
2 min read
DAY() Function in MySQL DAY() function : This function in MySQL is used to return the day of the month for a specified date (a number from 1 to 31). This function equals the DAYOFMONTH() function. Syntax : DAY(date) Parameter : This method accepts a parameter which is illustrated below : date : Specified date to extract th
1 min read
ELT() Function in MySQL In this article, we are going to cover ELT function with examples. In ELT function, number field will state that how many strings will be there. ELT function in MySQL is used to returns the string which is at index number specified in the argument list. In this function there is number field and str
1 min read
MySQL | CONV( ) Function The MySQL CONV() function is used for converting a number from one numeric base system to another. The value returned by the CONV() function is in the form of a string value. It accepts three parameters which are the value to be converted, the current numeric base system and the numeric base system
2 min read
AVG() Function in MySQL AVG() function : This function in MySQL is used to return the average value of the specified expression. Features : This function is used to find the average value of the specified expression.This function comes under Numeric Functions.This function accepts only one parameter namely expression.This
2 min read
MySQL | CAST( ) Function The MySQL CAST() function is used for converting a value from one datatype to another specific datatype. The CAST() function accepts two parameters which are the value to be converted and the datatype to which the value needs to be converted. The datatypes in which a given value can be converted are
3 min read
MySQL | CONVERT( ) Function The MySQL CONVERT() function is used for converting a value from one datatype to a different datatype. The MySQL CONVERT() function is also used for converting a value from one character set to another character set. It accepts two parameters which are the input value and the type to be converted in
2 min read
CEILING() Function in MySQL CEILING() function :This function in MySQL is used to return the smallest integer value that is greater than or equal to a specified number. For example, if the specified number is 4.6, this function will return the integer value of 5 that is greater than 4.6 or if a specified number is 5, this func
2 min read