The CONCAT_WS String Function in SQL Server 2017

In this article, we will be discussing about the CONCAT_WS string function, which was first introduced in SQL Server 2017

 

About the CONCAT_WS String Function in SQL Server

The CONCAT_WS string function, is function that was originally shipped with SQL Server 2017. This function, 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 of the CONCAT_WS Function

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:

The CONCAT_WS String Function in SQL Server 2017 and later (article on SQLNetHub)

 

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:

The CONCAT_WS String Function in SQL Server 2017 and later (article on SQLNetHub)

As you can see, all values in each record have been concatenated and they now have 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:

The CONCAT_WS String Function in SQL Server 2017 and later (article on SQLNetHub)

 

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.

 

Conclusion

 

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.

 

Learn essential SQL Server development tips – Enroll to our Online Course!

Check our online course titled “Essential SQL Server Development Tips for SQL Developers(special limited-time discount included in link).

Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!

Essential SQL Server Development Tips for SQL Developers - Online Course
(Lifetime Access, Q&A, Certificate of Completion, downloadable resources and more!)

Learn More

 

 

 

Did you find this article useful and interesting? Find hundreds of useful SQL Server programming/development articles in my eBook: “Developing with SQL Server (Second Edition)“.

Check our other related SQL Server Development articles.

Check out our latest software releases!

Subscribe to our newsletter and stay up to date!

 

Featured Online Courses:

 

Check our Related SQL Server Development Articles:

 

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

Loading...

Reference: SQLNetHub (https://www.sqlnethub.com)

© SQLNetHub