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!

Need to combine notes from multiple lines into one view 2

Status
Not open for further replies.

JordanCN

IS-IT--Management
Apr 12, 2006
77
US
I have an program based on SQL 2000 back end. The program use to have a note field nvarchar of 600 characters where you could enter a note like so:

This is a test
Test Line 2
Test Line 3

And it would end up looking like this in the table:

PartNumber Notes
---------- -----------------------
XYZ-1234 This is a test[CRLF]Test Line 2[CRLF]Test Line 3

Now they change the structure and re-organized the data so it is now:

PartNumber Line Notes
---------- ---- -----------------------
XYZ-1234 0001 This is a test
XYZ-1234 0002 Test Line 2
XYZ-1234 0003 Test Line 3

In order for me to check if the notes and other items on XYZ-1234 matched ABC-9876 I need to have all these notes grouped into one field in a view or something that I can query against with MS Access and Crystal Reports. Is there a way to get a view of the new table to simulate the old?
 
Yes, you can do this. You can use the STUFF() method. I'm not sure if it is available in SQL 2000 but I've used it before and it's very handy (although I thought it was pretty complex too - at least for what I was doing).

Here is a link to it:
I can post an example if you need me to.

Regards,

J
 
select distinct s1.partnumber,
STUFF((SELECT ' ' + s2.partnumber FROM dbo.table AS s2 WHERE s2.partnumber = s1.partnumber FOR XML PATH('')), 1, 1, '') AS CODES
from dbo.table s1


simi
 
Thanks, but this does not seem to be what I am looking for. I am not looking to substitute or modify the Part Number. I am looking to concatenate several records to appear as one

Right now when I run

Select * from PartNotes where PartNumber = "XYZ-124"

I get:

PartNumber Line Notes
---------- ---- -----------------------
XYZ-1234 0001 This is a test
XYZ-1234 0002 Test Line 2
XYZ-1234 0003 Test Line 3

But I want a view:

Select * from vwPartNotes where PartNumber = "XYZ-1234"

I get

PartNumber Notes
---------- -----------------------
XYZ-1234 This is a test[CRLF]Test Line 2[CRLF]Test Line 3

 
The advice given already assumes you are using SQL2005. Since you are using SQL2000, you'll need to use another approach. I suggest you read this thread:

thread183-1159740

It's an old thread, but it applies to SQL2000, so it should work out well for you. Pay particular attention, near the end, where a UDF is created.



-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top