Blogger :
Geekswithblogs.net
All posts :
All posts by Geekswithblogs.net
Category :
SQLXML
Blogged date : 2007 Dec 11
I came across this simple yet great way of translating a delimited string to XML using SQL 2005.
http://www.sqlservercentral.com/articles/XML/61618/
/*
Here is the string that we need to split
*/
DECLARE @str VARCHAR(100)
SET @str = '0001,0002,0003,0004,0005'
/*
I am converting the string to an XML structure by
inserting XML tags.
*/
DECLARE @x XML
SET @x = '<i>' + REPLACE(@str, ',', '</i><i>') + '</i>'
/*
Now we can apply XQuery to return a resultset
*/
SELECT x.i.value('.', 'VARCHAR(4)') AS Item
FROM @x.nodes('//i') x(i)
/*
Item
----
0001
0002
0003
0004
0005
(5 row(s) affected)
*/
