Solved

How to manipulate data from a SELECT statement, STUFF function

Posted on 2010-11-23
8
268 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 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

803 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