Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Create a comma separated list from column using select statement

Create a comma separated list from column using select statement

30 Jul 2024
Intermediate
163K Views
1 min read
Learn with an interactive course and practical hands-on labs

SQL Server Course

Create a comma-separated list from the column using the select statement: An Overview

Sometimes it is necessary to construct a comma-separated list of column values, such as a list of EmailIDs for mail distribution. In SQL Server, this can be accomplished by using COALESCE, as seen below. Consider using a SQL Server Tutorial or enrolling in a SQL Server Certification Course to gain additional insights and skills.

Read More - Top 50+ Database Interview Questions And Answers

Use of COALESCE to create a comma-separated list

Suppose we have the following data in the Employee table and we need to make a semicolon-separated list of EmailIDs to send mail, then we can use COALESCE as shown in below fig.

Here I am creating a semicolon(;) separated list. You can use a comma(,) in place of the semicolon to make the comma-separated list.

For SQL Server database mail setup and configuration and more on how to send mail from SQL Server database you can refer to this article.

Summary

In this article, I expose the use of COALESCE to make comma-separated or deli-meter-separated lists. I hope after reading this article you will know how to configure SQL Database Mail in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, questions, or comments about this article.

FAQs

In SQL Server, use the STRING_AGG() function with the column you want to concatenate & the delimiter (a comma).

Yes, you can do this by using the XML Path concatenation method or recursive CTEs to concatenate items with custom logic.

To exclude NULL values, just use a conditional statement within the aggregation function, like this: STRING_AGG(column_name, ',') WHERE column_name IS NOT NULL.

To exclude NULL values, just use a conditional statement within the aggregation function, like this: STRING_AGG(column_name, ',') WHERE column_name IS NOT NULL.

The STRING_AGG() method in SQL Server has a maximum input size limit of 4 MB, therefore the number of items that can be concatenated is determined by the size of the data.

Take our Sqlserver skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this