SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to concatenate many rows into one
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ericny
Starting Member

2 Posts

Posted - 02/13/2007 :  00:45:22  Show Profile  Visit ericny's Homepage  Reply with Quote
I have data that looks like this:

ID Value
1 Descr1
1 Descr2
1 Descr3

where Descr could range from 1 to 100 for each ID

The result set I need is:
Descr1,Descr2,Desc3...etc.
Thank you

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 02/13/2007 :  00:59:04  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Please remember that these concatenated strings only can hold 8000 characters.

declare @s varchar(8000)

select @s = left(isnull(@s + ',', '') + x.value, 8000)
from (select top 100 percent distinct value from yourtable1 order by value) as x

select @s


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16769 Posts

Posted - 02/13/2007 :  01:00:57  Show Profile  Reply with Quote
also see here http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx


KH

Go to Top of Page

ericny
Starting Member

2 Posts

Posted - 02/15/2007 :  23:30:18  Show Profile  Visit ericny's Homepage  Reply with Quote
Thank you.

Basically I came up with the following:

DROP FUNCTION dbo.ConcatDescr
go

CREATE FUNCTION dbo.ConcatDescr(@TXRCODE CHAR(8))
RETURNS VARCHAR(300)
AS
BEGIN
DECLARE @Output VARCHAR(300)
SET @Output = ''
SELECT @Output = CASE @Output
WHEN '' THEN MON_TEXT
ELSE @Output + ', ' + MON_TEXT
END
FROM PCLONG
WHERE TXRCODE = @TXRCODE
order by MON_PCH

RETURN @Output
END
GO

SELECT TXRCODE, dbo.ConcatDescr(TXRCODE)
FROM PCLONG
WHERE TXRCODE = '01100008'

The code above works to concatenate lines into one however it truncates data after 256 characters. I looked in help and it says that varchar can be up to 8000 chars. Is there something I am doing wrong?

Thank you again.

EricNY
www.zipdrugs.com
Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5509 Posts

Posted - 02/15/2007 :  23:33:42  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
"Is there something I am doing wrong?"

Yes, but it has nothing to do with your query. In Query Analyzer, Tools, Options, Results tab, set Maximum characters per column to some higher value, say, 8192.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.31 seconds. Powered By: Snitz Forums 2000