In this tutorial, I will create a utility function used for string
split and return in a table.
ALTER FUNCTION Splitstring_to_table ( @string NVARCHAR(MAX), @delimiter CHAR(1) ) RETURNS @output TABLE( id INT IDENTITY(1,1) PRIMARY KEY, data NVARCHAR(MAX) ) BEGIN DECLARE @start INT, @end INT SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) WHILE @start < LEN(@string) + 1 BEGIN IF @end = 0 SET @end = LEN(@string) + 1 INSERT INTO @output (data) VALUES(SUBSTRING(@string, @start, @end - @start)) SET @start = @end + 1 SET @end = CHARINDEX(@delimiter, @string, @start) END RETURN END
push string to function, see result
SELECT * FROM dbo.Splitstring_to_table('Bangladesh, America, German,',',')
Charindex :
In SQL Server (Transact-SQL), the CHARINDEX functions returns the location of a substring in a string. The search is NOT case-sensitive.
CHARINDEX( @delimiter, @string, [start_position] )
Substring :
In SQL Server (Transact-SQL), the SUBSTRING functions allows you
to extract a substring from a string.
CHARINDEX( @delimiter, @string, [start_position] )
Substring :
In SQL Server (Transact-SQL), the SUBSTRING functions allows you
to extract a substring from a string.
SUBSTRING( string, [start_position], length )
Comments
Post a Comment