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.
As you can see, we have broken down the words separated by commas.
Good luck to everyone in business and life.