Advertisement
infogulch

Untitled

Aug 31st, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.66 KB | None | 0 0
  1. -- splits a string by a delimiter string and returns each part in a table with a 1-based index in order of occurrence.
  2. -- it should be deterministic, no?
  3.  
  4. IF OBJECT_ID('dbo.ufn_Split',N'FN') IS NOT NULL DROP FUNCTION dbo.ufn_Split;
  5. GO
  6.  
  7. CREATE FUNCTION dbo.ufn_Split( @str nvarchar(max), @d nvarchar(5) )
  8. RETURNS nvarchar(max)
  9. --WITH RETURNS NULL ON NULL INPUT
  10. AS
  11. BEGIN
  12.     DECLARE @pos int = 1, @lpos int = 1, @id int = 0, @list nvarchar(max)
  13.     WHILE LEN(@str) >= @pos BEGIN
  14.         SET @id = @id+1; SET @pos = CHARINDEX(@d,@str,@lpos);
  15.         IF @pos = 0 SET @pos = LEN(@str)+1;
  16.         SET @List = SUBSTRING(@str,@lpos,@pos-@lpos);
  17.         SET @lpos = @pos+1;
  18.     END
  19.     RETURN @list
  20. END
  21. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement