How to split a delimited string to multiple rows using MSSQL

To split a delimited string to multiple rows using MSSQL you can use the snippet below.

Sample MSSQL

CREATE FUNCTION dbo.StringSplit
(
    @Input       NVARCHAR(MAX),
    @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
AS
    RETURN (
		SELECT Number = ROW_NUMBER() OVER (ORDER BY Number),
        Item FROM (
					SELECT Number, Item = LTRIM(RTRIM(SUBSTRING(@Input, Number, CHARINDEX(@Delimiter, @Input + @Delimiter, Number) - Number)))
					FROM (SELECT ROW_NUMBER() OVER (ORDER BY ao.[object_id])
					FROM sys.all_objects AS ao CROSS APPLY sys.all_objects) AS n(Number)
					WHERE Number <= CONVERT(INT, LEN(@Input))
					AND SUBSTRING(@Delimiter + @Input, Number, 1) = @Delimiter
				  ) AS item);
GO

How to Split a String at newline in C# and VB.NET

To Split a String at newline in C# and VB.NET you can use the following snippet.

Sample C#

public static IEnumerable<String> SplitAtNewLine(string input)
{
	return input.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
}

Sample VB.NET

Public Shared Function SplitAtNewLine(input As String) As IEnumerable(Of String)
	Return input.Split(New () {Environment.NewLine}, StringSplitOptions.None)
End Function