Solved

How to manipulate data from a SELECT statement, STUFF function

Posted on 2010-11-23
8
267 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now