Split string into table

Returns tag words (as table) in Microsoft SQL
Separator set to comma, but you can modify it in code
Was part of a big project in .NET

Opposite of Rows 2 Cell

CodeFunctionName
What is this?

Public

Tested

Original Work
/****** Object: UserDefinedFunction [dbo].[SplitString] Script Date: 12/18/2018 10:35:11 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <ANmar Amdeen >
-- Create date: <2018-04-04 >
-- Description: <Splits strng into table, having row per word (separator is a comma ,) >
-- =============================================
CREATE FUNCTION [dbo].[SplitString] ( @stringToSplit VARCHAR(MAX) )
RETURNS
@returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN

DECLARE @name NVARCHAR(255)
DECLARE @pos INT

WHILE CHARINDEX(',', @stringToSplit) > 0
BEGIN
SELECT @pos = CHARINDEX(',', @stringToSplit)
SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

INSERT INTO @returnList
SELECT LTrim(RTRIM( @name))

SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
END

INSERT INTO @returnList
SELECT ltrim(RTrim(@stringToSplit))

RETURN
END

GO

stringToSplit

Views 3,923

Downloads 2,168

CodeID
DB ID