devxlogo

Retrieve a Concatenated String from One Column of a Table Without Using the Cursor

Sometimes, you need to retrieve a concatenated string from one column of a table without using the cursor. To do this, you can use string aggregation. Here’s how:

  1. Create & fill the table using this script:
    --Create Table Cust(CustID int,CustName varchar(20))Insert into Cust Values(1,'Rohit')Insert into Cust Values(2,'Amit')Insert into Cust Values(3,'Sumit')Insert into Cust Values(3,'Punit')
  2. Execute this script:
    Declare @Names Varchar(2000)Select @Names=Isnull(@Names+','+CustName,CustName)From CustSelect @Names
  3. You will get this result:
    Rohit,Amit,Sumit,Punit(1 row(s) affected)

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.