Split Function for T-SQL using XML
Posted by Tim on July 29, 2009 in SQL Server |
Here is some sample code to split a string and return the results via a table.
CREATE FUNCTION [dbo].[split]
(
@del char(1),
@str varchar(max)
)
RETURNS @tResult TABLE
(
val varchar(max)
)
AS
BEGIN
declare @xml xml
set @xml = N’<root><r>’ + replace(@str,@del,’</r><r>’) + ‘</r></root>’
insert into @tResult(val)
select
r.value(‘.’,'varchar(max)’) as item
from @xml.nodes(‘//root/r’) as records(r)
RETURN
END
1 Comment
Leave a Reply
You must be logged in to post a comment.



[...] found the following function using XML and am reposting it here for future reference. Thanks to http://blog.codelab.co.nz/2009/07/29/split-function-for-t-sql-using-xml/ for the [...]