Open In App

CONCAT_WS() Function in SQL Server

Last Updated : 02 Dec, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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(' - ', 'GeeksforGeeks', 'computer', 'science', 'portal');
Output :
GeeksforGeeks - 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 -
PLAYERIDPLAYERNAMENICKNAME
45Rohit SharmaHit Man
18Virat KohliChiku
7MS DhoniMSD
SELECT 
    PLAYERNAME, 
    NICKNAME, 
    CONCAT_WS(' - ', PLAYERNAME, NICKNAME) Name_with_NickName
FROM 
    Player_Details
Output :
PLAYERNAMENICKNAMEName_with_NickName
Rohit SharmaHit ManRohit Sharma - Hit Man
Virat KohliChikuVirat Kohli - Chiku
MS DhoniMSDMS Dhoni - MSD

Next Article
Article Tags :

Similar Reads