Solved

How to use a loop or a cursor to list values in a horizontal delimited string (e.g. 1001, 1002, 1003)?

Posted on 2008-10-31
5
524 Views
Last Modified: 2012-05-05
I am trying to find some help/a way to output a horizontal comma delimited list of associated items in a table.

Example: A user wants to see a few documents tied to each company in a database for verification that the document have the correct company name listed.

DocumentID      CustomerID
1001            1
1002            1
1003            1
1004            1
1005            1
1006            2

CustomerID      Customer Name
1            IBM
2            National, Inc.


Customer Name      ListofTop3Documents
IBM            1001, 1002, 1003
National, Inc.            1006
0
Comment
Question by:endrec
  • 4
5 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 22853599
CREATE FUNCTION [dbo].[fnSplit]
(
      -- Add the parameters for the function here

      @list nvarchar(2000),
      @delimiter nvarchar(5)

)
RETURNS
@output   TABLE
(
      -- Add the column definitions for the TABLE variable here

data VARCHAR(256)
)
AS
BEGIN
      -- Fill the table variable with the rows for your result set


DECLARE @start INT, @end INT
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @list)

    WHILE @start < LEN(@list) + 1
      BEGIN
        IF @end = 0
            SET @end = LEN(@list) + 1

        INSERT INTO @output (data)
        VALUES(SUBSTRING(@list, @start, @end - @start))
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @list, @start)
    END

      RETURN
END
0
 
LVL 13

Expert Comment

by:sm394
ID: 22853609
then

select * from dbo.fnSplit('1001, 1002, 1003',',')
0
 

Author Comment

by:endrec
ID: 22853819
Oh, this is the end result that it desired (using the first two tables as the data sources):

Customer Name      ListofTop3Documents
IBM            1001, 1002, 1003
National, Inc.            1006
0
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 22854037
CREATE FUNCTION [dbo].[fnDocumentIDs](@CustomerID INT)
RETURNS NVARCHAR(1000)
AS
BEGIN


DECLARE @DocIDs nvarchar(1000), @delimiter char
SET @delimiter = ','

SELECT     @DocIDs= COALESCE(@DocIDs+@delimiter,'')+ YourTable.DocumentID
FROM   YourTable
WHERE CustomerID=@CustomerID

RETURN RTRIM(LTRIM(@DocIDs))

END


--------------
next query will follow
0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 500 total points
ID: 22854065
select    CustomerName,
[dbo].[fnDocumentIDs](CustomerID) as ListofTop3Documents
from tblCustomers
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQLCMD Syntax 2 15
Parse this column 6 27
Help With SQL Query 9 32
SQL Error when trigger fires in SQL Server 2008 R2 6 16
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question