Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert multiple rows to one record or variable 1

Status
Not open for further replies.

Z1

Programmer
May 8, 2000
14
US
I have a varible table with one column and multiple row. I want one row with the combination of all rows. Below is the example:
E01
E02
E03
Output : E01,E02,E03

 
Code:
Declare @Temp Table(Data VarChar(10))

Insert Into @Temp Values('E01')
Insert Into @Temp Values('E02')
Insert Into @Temp Values('E03')

Declare @Output VarChar(1000)

Select 	@Output = Coalesce(@Output + ',', '') + Data
From	@Temp

Select @Output

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks George for your help. You have my star for this week.
 
Glad to help.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks rafrancisco,

I was using as follows to get my desired result as your web site indicates:
DECLARE @CustomerIDs VARCHAR(8000)
SELECT @CustomerIDs = ISNULL(@CustomerIDs + ',', '') + [CustomerID]
FROM [dbo].[Customers]
ORDER BY [CustomerID]
PRint @CustomerIDs

BUT I have trouble after I add ORDER clause. The above deletes all the records and keep the last record only in the variable. This works perfect in the table where there is no fill factor. I have fill factor 80 for the index in my table (which I am using) and the result is incomplte.
Example:
ID Error
1 E21
1 E12
1 E03
Result with above code : 1 E03
As soon as I remove "Order by" clause the result comes correct(1 E21,E12,E03) . Any idea?

Z1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top