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:
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:
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:
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!
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:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- The Philosophy and Fundamentals of Computer Programming
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Check our Related SQL Server Development Articles:
- Essential SQL Server Development Tips for SQL Developers
- How to Import and Export Data in SQL Server
- Listing all Tables of a Linked Server’s Database
- How to Import and Export Unstructured Data in SQL Server – FileTables
- How to Import and Export Unstructured Data in SQL Server – FILESTREAM
- How to Import and Export Unstructured Data in SQL Server – The IMAGE Datatype
- …more
Rate this article:
Reference: SQLNetHub (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.