The CONCAT_WS String Function in SQL Server 2017

SQLEBooks.com
Share on Facebook2Share on Google+0Tweet about this on TwitterShare on LinkedIn3Share on Reddit1Pin on Pinterest0

The CONCAT_WS String Function in SQL Server 2017 is a new function that provides more flexibility for string operations.

Definition
CONCAT_WS concatenates a variable number of arguments setting as a delimiter the first argument in the function.

Examples
Before checking out some examples of using CONCAT_WS, let’s create a temporary table with some sample data:

CREATE TABLE #tmpTable(
id int PRIMARY KEY,
code VARCHAR(50),
descr VARCHAR(100)
);
GO

INSERT INTO #tmpTable
VALUES	(1,'code1','descr1'),
        (2,'code2','descr2'),
        (3,'code3','descr3'),
        (4,'code4','descr4'),
        (5,'code5','descr5');
GO

 

Let’s take a look at the sample data we have just created:

CONCAT_WS Example in SQL Server 2017

 

 

Now let’s concatenate the columns in each record in the above table  by running the below query:

SELECT CONCAT_WS('-',id,code,descr) FROM #tmpTable;
GO

And here are the results:

CONCAT_WS Example in SQL Server 2017

 

As you can see all column values in each record have been concatenated having the character ‘-‘ as a delimiter.

Another use of CONCAT_WS is that you can easily generate CSV files. Based on our example, let’s export the table’s contents in CSV format using semicolon (;)

So, prior to running the query, first in Management Studio query window, right-click and select to display the results of the query to Text (Results To –>  Results to Text).

OK, now let’s run the query:

SELECT CONCAT_WS(';',id,code,descr) as ConcatValue FROM #tmpTable;
GO

And here are the results of the above query:

CONCAT_WS Example in SQL Server 2017

 

As you can see, the concatenated strings are delimited with semicolon and you can easily write the results into a new file, thus automatically creating a CSV file.

Recommended for you: My latest eBook on SQL Server development.

CONCAT_WS is a handy new string function in SQL Server and Azure SQL Database. It provides additional flexibility when it comes to string manipulation processes in SQL Server. In subsequent posts, we will explore more programmability features in SQL Server 2017 and Azure SQL Database.

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)

Loading...

Reference: SQLEBooks.com (https://www.sqlebooks.com)

 


Recommended eBooks on SQL Server:

Developing with SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Developing SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
About artemakis 6 Articles
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.