Solved

How to manipulate data from a SELECT statement, STUFF function

Posted on 2010-11-23
8
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 6

Accepted Solution

by:
subhashpunia earned 400 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
Independent Software Vendors: 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 100 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

690 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