CONCAT_WS() Function in MySQL
Last Updated :
03 Dec, 2020
CONCAT_WS() :
This function in MySQL helps in joining two or more strings along with a separator. The separator must be specified by the user and it can also be a string. If the separator is NULL, then the result will also be NULL.
Syntax :
CONCAT_WS(separator, string1, string2, ...)
Parameters :
- separator -
A separator which will be added between the strings while concatenation string1, string2, etc.
- [string1, string2 ...] -
The input strings which needed to be concatenated.
Return :
It will return a new string, after concatenating all given strings, along with a specified separator. And if all input strings are NULL, then the result will be NULL. If the separator is NULL, it will return NULL.
Example 1 :
Concatenating 2 strings using CONCAT_WS Function as follows.
SELECT CONCAT_WS(": ", "Geek ", "Vansh ") AS ConcatWsStr;
Output :
Example-2 :
Concatenating 3 strings using CONCAT_WS Function as follows.
SELECT CONCAT_WS("@ ", "Geek ", "Vansh ", 13 ) AS ConcatWsStr;
Output :
ConcatWsStr |
---|
Geek @ Vansh @ 13 |
Example-3 :
Concatenating a NULL string using NULL separator as follows.
SELECT CONCAT_WS(NULL, NULL, "Vansh ", 13 ) AS ConcatWsStr;
Output :
Example-4 :
Concatenating the columns of a table using CONCAT_WS Function as follows.
Creating an Employee table :
CREATE TABLE Emp(
Employee_Id INT AUTO_INCREMENT,
FirstName VARCHAR(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Residence VARCHAR(50) NOT NULL,
Salary INT NOT NULL,
PRIMARY KEY(Employee_Id )
);
Inserting data into the Table :
INSERT INTO Emp(FirstName, LastName, Residence, Salary )
VALUES
('Animesh', 'Garg', 'Delhi', 70000 ),
('Neshu', 'Sharma', 'Nepal', 73000 ),
('Aryan', 'Sharma', 'WestBengal', 72000 ),
('Abdul', 'Ali', 'Delhi', 73000 ),
('Seema', 'Sharma', 'Bihar', 70000 ) ;
To verify used the following command as follows.
Select * From Emp;
Output :
Employee_Id | FirstName | LastName | Residence | Salary |
---|
1 | Animesh | Garg | Delhi | 70000 |
2 | Neshu | Sharma | Nepal | 73000 |
3 | Aryan | Sharma | WestBengal | 72000 |
4 | Abdul | Ali | Delhi | 73000 |
5 | Seem | Sharma | Bihar | 70000 |
Now, concatenate FirstName and LastName of given Emp table using '_' as a separator to form a new column as FullName.
SELECT CONCAT_WS('_', FirstName, LastName) AS FullName
From Emp;
Output :
FullName |
---|
Animesh_Garg |
Neshu_Sharma |
Aryan_Sharma |
Abdul_Ali |
Seema_Sharma |
Similar Reads
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
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
CONCAT_WS() Function in SQL Server
CONCAT_WS() : This function concatenates two or more strings together with a separator. Syntax : CONCAT_WS(separator, input_string1, input_string2, [...input_stringN]); Parameter : This method accepts two-parameters as mentioned above and described below as follows. separator - It is an expression o
1 min read
Node.js MySQL CONCAT_WS() Function
CONCAT_WS() function is a built-in function in MySQL that is used to concatenate a set of strings with a commonly given separator. Syntax: CONCAT_WS(separator, string_1, string_2, ...)Parameters: It takes two parameters as follows: separator: This separator will be used to concatenate strings.string
2 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
BIT_COUNT() function in MySQL
BIT_COUNT() function in MySQL is used to return the number of bits that are active in the given input. Active bits can be counted as a number of 1 presented in a binary number.Syntax : BIT_COUNT(number) Parameter : This method accepts only one parameter. number - Input integer whose number of active
2 min read
MySQL | Group_CONCAT() Function
The GROUP_CONCAT() function in MySQL is an aggregation function that combines data from multiple rows into a single string. It is particularly useful for aggregating summaries, such as combining related information into a single field for better readability or reporting. In this article, we will exp
4 min read
MySQL | CONNECTION_ID( ) Function
The MySQL CONNECTION_ID() function is used for return the connection ID for a current connection in MySQL. The connection ID used to establish a connection to a database is unique for every connection among the connected clients. The CONNECTION_ID() function does not require any parameters or argume
1 min read
EXPORT_SET() function in MySQL
EXPORT_SET() : This function helps to return a string which will show the bits in number. The function requires 5 arguments for its functioning. The function converts first argument i.e integer to binary digits, then returns âonâ if binary digit is 1 and âoffâ if binary digit is 0. Syntax : EXPORT_S
2 min read
CRC32() Function in MySQL
CRC32() function in MySQL is used to compute cyclic redundancy value. It returns NULL if the argument is NULL otherwise, it returns a 32-bit unsigned value after computing the redundancy. Syntax : CRC32(expr) Parameter : This method accepts only one parameter. expr -It is a string whose CRC32 value
2 min read