Combine multiple SQL2000 results into single string

Ok... so I have these tables

Line_Item
ID   |  NAME  
------------------
1         ITEM1
   
Line_Item_Text
L_I_ID   |  TYPE  |   TXT
-------------------------------------
1                 N         NOTE1
1                 N         NOTE2
1                 N         NOTE3
If Line_Item_Text TYPE ='N' it contains the text for Notes, which can contain multiple entries for any give Line_Item (L_I_ID).

So... In a view, joined to Line_Item I want to return all notes as single string delimited with spaces.

VIEW RETURNS

ID  |   NAME   |   NOTES
-------------------------------------------------------------
1        ITEM1        NOTE1 NOTE2 NOTE3

How do I concatenate the sub results like that in SQL?

function or the like would be fine.....

there are multiple txt types and i want to apply it across the board the sample above is extremely simplified.
GrizzlerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

winheimCommented:
I would generate a function like this

ALTER FUNCTION fn_Combine
(
	@ID int 
)
RETURNS nvarchar(max) 
AS

BEGIN

	DECLARE @sReturn nvarchar(max) = ''

	SELECT @sReturn  = @sReturn + ' ' + lit.TXT
	FROM Line_Item li
	INNER JOIN Line_Item_Text lit ON Li.ID = lit.L_I_ID
	WHERE li.ID = @ID

	RETURN RTrim(@sReturn)
END	

Open in new window


and then I would call the function like this

SELECT li.ID , li.Name ,  dbo.fn_Combine(li.ID)  AS Notes
FROM Line_Item li

Open in new window


is this a solution which is working with you?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrizzlerAuthor Commented:
I had already got there,  but your answer was right on the money....

Here's what I did (names are a little different as it is actual)

ALTER FUNCTION [dbo].[fnGetConcText](@LID INT, @TYPE CHAR(4))

RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Names VARCHAR(8000)
set @Names=''

SELECT  @Names =@Names + ' *** ' + TXT
FROM LINE_ITEM_TEXT
WHERE TXT <>'' and txt is not null and LINE_ITEm_ID=@LID and TYPE=@TYPE

Set @NAMES= substring(@Names,6, 7994)

RETURN @NAMES
END


It deals with nulls and uses a different delimiter. It is running in test as we speak....
Curious in sql2000 what will happen if I exceed varchar(8000) and read somewhere not to use varchar(max)... Any more comments?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.