How to create function for split string in SQL
Function accepts two parameters first the concatenated string and second the delimiter character.
CREATE FUNCTION [dbo].[Split](@String nvarchar(4000),@Delimiter char(1))
RETURNS @Results TABLE (Items nvarchar(4000))
AS
BEGIN
DECLARE @IND INT
DECLARE @SLICE nvarchar(4000)
SELECT @IND = 1
IF @String IS NULL RETURN
WHILE @IND !=0
BEGIN
SELECT @IND = CHARINDEX(@Delimiter,@STRING)
IF @IND !=0
SELECT @SLICE = LEFT(@STRING,@IND-1)
ELSE
SELECT @SLICE = @STRING
INSERT INTO @Results(Items) VALUES(@SLICE)
SELECT @STRING = RIGHT(@STRING,LEN(@STRING)-@IND)
IF LEN(@STRING) = 0 BREAK
END
RETURN
END
How to execute above function
select items from dbo.split('Split string by space','')
0 Comment(s)