Function to Split Text Containing Comma Separated Words in SQL Server

Hello everyone,

In this article, I will try to give information about the function that splits text containing comma-separated words in SQL Server.

In SQL Server you may want to break up comma separated words in some cases.

You can easily do this by creating and using the function below.

CREATE FUNCTION VirgulleAyrilmisMetinleriParcalama (@String VARCHAR(50), @Karakter CHAR(1))
RETURNS @Tablo TABLE (
	Kolon VARCHAR(50)
)
AS
BEGIN
	SET @String = @String + @Karakter
	WHILE (CHARINDEX(@Karakter, @String)) > 0
	BEGIN
	DECLARE @baslangic INT = 1
	DECLARE @son INT
	SET @son = CHARINDEX(@Karakter, @String)
	INSERT INTO @Tablo
		SELECT
			SUBSTRING(@String, @baslangic, @son - 1)
	SET @String = SUBSTRING(@String, @son + 1, LEN(@String))
	END
	RETURN
END

--Using the Function

SELECT
	*
FROM dbo.VirgulleAyrilmisMetinleriParcalama('yavuz, selim, kart', ',')

When you create and run the above function, you will see a result similar to the one below.

Function to Split Text Containing Comma Separated Words in SQL Server

As you can see, we have broken down the words separated by commas.

Good luck to everyone in business and life.

55 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!