Skip to content

Instantly share code, notes, and snippets.

@RickyLin
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickyLin/3a9d896b235ed0946733 to your computer and use it in GitHub Desktop.
Save RickyLin/3a9d896b235ed0946733 to your computer and use it in GitHub Desktop.
Split string in Sql Server - XML approach
-- it comes from this great post: http://sqlperformance.com/2012/07/t-sql-queries/split-strings
-- this way turns out not to be very efficient
CREATE FUNCTION dbo.SplitStrings_XML
(
@List NVARCHAR(MAX),
@Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
FROM
(
SELECT x = CONVERT(XML, '<i>'
+ REPLACE(@List, @Delimiter, '</i><i>')
+ '</i>').query('.')
) AS a CROSS APPLY x.nodes('i') AS y(i)
);
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment