I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 159, comments - 1467, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

SQL Server 2005: Concat values XML Style

A few days ago i showed how to split string with XML.

Now it's time for concatenation with XML.

 

DECLARE @t TABLE (col VARCHAR(10))

INSERT into @t
select 'aaaa' UNION ALL
select 'bbbb' UNION ALL
select 'cccc' UNION ALL
select null UNION ALL
select 'dddd'

SELECT * FROM @t

SELECT 
(
    SELECT  col + ', ' as [text()]
    FROM    @t
    ORDER BY col DESC
    FOR XML PATH('') 
) AS MyCsvList


Both of these (split and concat) methods aren't really anything special. They're just a new way of solving old problem.

But whenever i need them i have to look them up, so where else to keep them handy than here, right? :)

 


kick it on DotNetKicks.com

Print | posted on Friday, June 01, 2007 9:49 AM

Feedback

# re: SQL Server 2005: Concat values XML Style

If you want to get rid of the last comma

SELECT LEFT(MyCsvList,LEN(MyCsvList)-1)
FROM (SELECT
(
SELECT col + ', ' as [text()]
FROM @t
ORDER BY col DESC
FOR XML PATH('')
) AS MyCsvList) z


Denis
6/1/2007 11:11 AM | Denis the SQL Menace

# re: SQL Server 2005: Concat values XML Style

thanx D-Man :)

I usually just trim it on the client, though.
6/1/2007 11:13 AM | Mladen

# re: SQL Server 2005: Concat values XML Style

Yes, because we all know what Celko says "Formatting should be done on the client" ;-)
6/1/2007 12:07 PM | Denis the SQL Menace

# re: SQL Server 2005: Concat values XML Style

Also see
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254
for more formatting stuff with FOR XML PATH
12/9/2007 10:57 PM | Peso

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 8 and type the answer here:

Powered by: