Introduction
Hello Everyone,
I am going to take advantage of this time to write code for a split comma separated string without using functions. In general, we face this type of scenario in real working conditions, and we can expect this in interview questions. Now, I came up with different resolutions for this. We can use Recursive CTE to split a comma-separated string in SQL. Instead of a string, we can use columns in our tables also. In my current project, I got raw data in a comma-separated string format, and I used it to split the string into rows and insert them into my tables.
- declare @a varchar(100)
- set @a = 'vinay,talapaneni,Hello,HI'
- ;with cte as(select STUFF(@a,1,CHARINDEX(',',@a),'') as number,
- convert(varchar(50),left(@a, CHARINDEX(',',@a)-1 )) col1
- union all
- select STUFF(number,1,CHARINDEX(',',number+','),'') number,
- convert(varchar(50),left(number,(case when CHARINDEX(',',number) = 0 then len(number) else CHARINDEX(',',number)-1 end)) )col1
- from cte where LEN(number) >0
- )
- select col1 from cte

Sanjay KumarPosted Feb 12, 2021, 10:39 AM
Nice article
Zulqadar IdrishiPosted Aug 28, 2020, 1:40 PM
Good Article!
Jay Krishna ReddyPosted Aug 28, 2020, 12:14 PM
Keep up the good work.!