?
Solved

How to manipulate data from a SELECT statement, STUFF function

Posted on 2010-11-23
8
Medium Priority
?
276 Views
Last Modified: 2012-08-13
I have a select that returns:
EQUIPMENT
Motor
Gear Box
Brake
Wheel

I want to show it as:
Motor-Gear Box-Brake-Wheel

I got started with the stuff function but doesn't seem to work. Can someone please help?
Can you also explain to me the xml path and stuff function.
(select distinct
	stuff((select ', ' + EQPMNT_NAME from EQUIPMENT WHERE EQPMNT_KEY=er.EQPMNT_KEY for xml path('')), 1, 2, '') As EQUIPMENT
FROM EQUIPMENT_REQUEST er 
WHERE RQUST_KEY=70)

Open in new window

0
Comment
Question by:codemonkey2480
  • 5
  • 2
8 Comments
 
LVL 6

Accepted Solution

by:
subhashpunia earned 1600 total points
ID: 34195965
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34195969
Try this


select distinct (stuff((select ', ' + EQPMNT_NAME from EQUIPMENT WHERE EQPMNT_KEY=er.EQPMNT_KEY for xml path('')), 1, 2, '')) As EQUIPMENT
FROM EQUIPMENT_REQUEST er
WHERE RQUST_KEY=70
0
 

Author Comment

by:codemonkey2480
ID: 34195997
Brichsoft,
Thanks for the reply. How is this different from my query?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34196024
Hi,

not much difference.
Just ( difference.

You opened before select.

I opened after distinct.

normally,main query never within (

( used for sub query or when you wanted to join...

your main select should not be within (

- Bhavesh
0
 

Author Comment

by:codemonkey2480
ID: 34196068
I get the same results, list of equipment like:
EQUIPMENT
Motor
Gear Box
Brake
Wheel


0
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 400 total points
ID: 34196141
Try this.


Declare @Table1 Table
(
      EQUIPMENT_KEY INT,
      EQUIPMENT varchar(50) not null
)

Insert @Table1
Select 1,'Motor'
Union All
Select 2,'Gear Box'
Union All
Select 3,'Brake'
Union All
Select 4,'Wheel'




Select * from @Table1

Select Distinct      
      (Stuff((Select ', ' + EQUIPMENT From @Table1 T2 FOR XML PATH('')),1,2,'')) as EQUIPMENT
From @Table1 T1
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34196145

select distinct (stuff((select ', ' + EQPMNT_NAME from EQUIPMENT for xml path('')), 1, 2, '')) As EQUIPMENT
FROM EQUIPMENT_REQUEST er
WHERE RQUST_KEY=70
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34196188
Hi,

It wont come....

see,you wanted EQUIPEMENT in 1 Row
You passing

Where T2.EQUIPMENT_KEY = T1.EQUIPMENT_KEY

as EQUIPMENT_KEY is unique,your equipment wont come comma seprated
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question