January 8, 2013 at 2:10 am
hi frieds i have small doubt in sql server plese tell me how to solve this
table data contains like
id , name
1 , a
1 , b
1 , c
2 , a
2 ,b
so i want out put like
id ,name
1 , abc
2 , ab
plese tell me how to write query in sql server
January 8, 2013 at 2:20 am
January 8, 2013 at 2:36 am
Hi,
Try this..
Hope this will help
SELECT OutTab.ID ,
Names =
STUFF ( ( SELECT ''+InrTab.Name
FROM [empInfo] InrTab
WHERE InrTab.id = OutTab.id
ORDER BY InrTab.id
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)')
, 1,0,SPACE(0))
FROM [empInfo] OutTab
GROUP BY OutTab.id;
January 8, 2013 at 2:43 am
Always post table defintion along with test data to get faster resultsl.people here steal their time to help others , sometimes they dont have time to frame everything here.please see the link below my signature. 🙂
hope this helps you
declare @t table
(id int , chr char(1))
insert into @t
select 1, 'a' union
select 1, 'h' union
select 1, 'y' union
select 4, 'k' union
select 4, 'f'
select * from @t
select distinct id, STUFF((select '' + cast(chr as nvarchar(20)) from @t where id= t.id FOR XML PATH('')),1,0,'')
from @t t
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
January 8, 2013 at 6:00 am
hi why here we use XML PATH('')),1,0,''
plese tell me some explnation to use of xml path
January 8, 2013 at 7:47 am
asranantha (1/8/2013)
hi why here we use XML PATH('')),1,0,''plese tell me some explnation to use of xml path
BOL has the answer to this and MANY MANY more questions.
http://msdn.microsoft.com/en-us/library/ms178107.aspx
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 8, 2013 at 8:06 am
This article should help some as well: http://www.sqlservercentral.com/articles/comma+separated+list/71700/.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply