Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get concatenated value for a column without cursor


Get concatenated value for a column without cursor

Author
Message
Nitin Bhargava
Nitin Bhargava
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3051 Visits: 78
Comments posted to this topic are about the item Get concatenated value for a column without cursor
nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
Here's a slightly more succinct version inspired by code from this article http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

SELECT t1.col1,
(
SELECT
CASE WHEN ROW_NUMBER() OVER(ORDER BY col1, col2)=1 THEN '' ELSE ',' END + col2
FROM
TestTable
WHERE
col1 = t1.col1
ORDER BY
col1 ,col2
FOR XML PATH('')
)
FROM
TestTable t1
GROUP BY
col1



--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

ricky70rana
ricky70rana
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 40
Your code is not working. It 's giving error ColRowNo is invalid Column. Can you please try to remove that error from your code. I executed that code in sql server 2008
Nitin Bhargava
Nitin Bhargava
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3051 Visits: 78
Please check if following statement is missing from your script -

Alter Table TestTable Add ColRowNo int

if it is missing, then add it next to statement "select * from TestTable"
it will work.
tim.pinder
tim.pinder
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 201
I'm getting a different error:
Update TestTable 
Set ColRowNo = RowNo
From
(
    Select RowID, Row_Number() Over (Partition By Col1 Order by RowID) RowNo From TestTable
) B Where TestTable.RowID = B.RowID
;



gives this:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ' '.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'.

Sad
Wesley Norton
Wesley Norton
SSC Veteran
SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)

Group: General Forum Members
Points: 273 Visits: 442
Tim, I had the same problem. I just removed all the offending whitespace and it works fine now:

---------------------------------------------------------------------
-- Create Table and Insert data
---------------------------------------------------------------------
if object_id('tempdb..TestTable')is not null drop table TestTable
Create Table TestTable (RowID Int Identity(1, 1), Col1 int, Col2 char(3))

Insert TestTable Values (1, 'ABC')
Insert TestTable Values (1, 'DEF')
Insert TestTable Values (1, 'GHI')
Insert TestTable Values (2, 'JKL')
Insert TestTable Values (2, 'MNO')
Insert TestTable Values (3, 'PQR')
Insert TestTable Values (3, 'STU')

select * from TestTable
---------------------------------------------------------------------
-- Add TestTable column and Insert Row Number
---------------------------------------------------------------------
Alter Table TestTable Add ColRowNo int

Update TestTable
Set ColRowNo = RowNo
From
(Select RowID
, Row_Number() Over (Partition By Col1 Order by RowID) RowNo
From TestTable
)as B Where TestTable.RowID = B.RowID
;
---------------------------------------------------------------------
-- Concatenate Col2 in comma seperated form Group by Col1
---------------------------------------------------------------------
With CTE (X, Y, Z) AS
(Select Col1, Convert(Varchar,Col2), ColRowNo From TestTable Where ColRowNo = 1
Union All
Select Col1, Convert(Varchar, Y + ',' + Convert(Varchar, Col2)) Y, ColRowNo
From TestTable
Inner Join CTE on Col1 = X and ColRowNo = Z + 1
)

Select X Col1, Y [Concatenated Col2] From CTE
Inner Join
(Select Col1, Max(ColRowNo) ColRowNo From TestTable Group by Col1
) TestTable on X = Col1 and Z = ColRowNo
Order by X
OPTION (MAXRECURSION 2);

Drop Table TestTable
GO


Robert.Smith-1001156
Robert.Smith-1001156
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 122
Virtuoso coding, certainly, but why not take advantage of SQL Server's ability to create custom aggregation functions? Microsoft's example can be found here:

http://msdn.microsoft.com/en-us/library/ms182741.aspx
vikingDBA
vikingDBA
SSC-Enthusiastic
SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)SSC-Enthusiastic (198 reputation)

Group: General Forum Members
Points: 198 Visits: 929
Mine isn't nearly as fancy, and it sticks a dangling comma on the end, but when I just want a quick listing of all columns in a table (say, for a select statement where they are in Ordinal Position order), I use the following (just change the <tablenamehere> to a real table name):



DECLARE @colline varchar(4000)
SET @colline = ''


SELECT @colline=@colline+COLUMN_NAME + ',' FROM information_schema.columns
WHERE TABLE_NAME = '<tablenamehere>'
ORDER BY ORDINAL_POSITION

SELECT @colline
Nitin Bhargava
Nitin Bhargava
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3051 Visits: 78
It is not mainly for comma seperated column name. It can be use when you want comma seperated data on grouping of some other field. Please see the output resultset 1 and resultset 2.
tim.pinder
tim.pinder
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 201
Thanks Wesley,

That certainly makes it easier to parse

:-D
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search