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 of any character type like char, nchar, nvarchar, or varchar. - input_string –
It is an expression of any type. The input_strings to add together.
Returns :
It returns a concatenated string value.
Example-1 :
Use ‘ – ‘ to separate the concatenated string values.
SELECT CONCAT_WS(' - ', 'w3wiki', 'computer', 'science', 'portal');
Output :
w3wiki - computer - science - portal
Example-2 :
Use ‘ ‘ to separate the concatenated string values.
SELECT CONCAT_WS(' ', 'Hardik', 'Pandya') Your_Name;
Output :
Your_Name |
---|
Hardik Pandya |
Example-3 :
Using CONCAT_WS() with NULL values.
SELECT CONCAT_WS(', ','DN Block', 'Bidhannagar', NULL, 'Kolkata', NULL, 700091) AS Your_Address;
Output :
Your_Address |
---|
DN Block, Bidhannagar, Kolkata, 700091 |
Example-4 :
Using CONCAT_WS() with table columns.
Table -Player_Details –
PLAYERID | PLAYERNAME | NICKNAME |
---|---|---|
45 | Rohit Sharma | Hit Man |
18 | Virat Kohli | Chiku |
7 | MS Dhoni | MSD |
SELECT PLAYERNAME, NICKNAME, CONCAT_WS(' - ', PLAYERNAME, NICKNAME) Name_with_NickName FROM Player_Details
Output :
PLAYERNAME | NICKNAME | Name_with_NickName |
---|---|---|
Rohit Sharma | Hit Man | Rohit Sharma – Hit Man |
Virat Kohli | Chiku | Virat Kohli – Chiku |
MS Dhoni | MSD | MS Dhoni – MSD |
Contact Us