Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help !!

Author  Topic 

kjs
Starting Member

3 Posts

Posted - 2013-04-25 : 10:47:38
Hi,

I have a requirement where i need to display the result set from row to column with space seperated values.

My query and result set :

select distinct filename from FileDetails where filetype='PST';

FileName
-----------------
09801001
09801002
09801003
09801004
09802001

Now, i need a query to write the result set in below format with space separated: The result set is dynamic and it changes depending upon the data.

09801001 09801002 09801003 09801004 09802001

Any help please !! Thanks in advance..

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-25 : 11:03:06
[code]
SELECT STUFF((SELECT ' ' + FileName FROM Table FOR XML PATH('')),1,1,'')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

kjs
Starting Member

3 Posts

Posted - 2013-04-25 : 11:32:39
quote:
Originally posted by visakh16


SELECT STUFF((SELECT ' ' + FileName FROM Table FOR XML PATH('')),1,1,'')


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Sorry I didnt get: what is XML PATH ??

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-25 : 11:45:09
it creates a XML document from resultset. In this case its used to create a dummy xml document which will contain the delimited list

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

kjs
Starting Member

3 Posts

Posted - 2013-04-25 : 20:40:10
That was awesome!!! It worked as I needed it.. Thanks :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-26 : 00:17:28
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -