Last active
August 29, 2015 14:19
-
-
Save RickyLin/3a9d896b235ed0946733 to your computer and use it in GitHub Desktop.
Split string in Sql Server - XML approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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