how can split a strin in sql using comaa delimiter
thanks in advance
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Sep 1, 2011, 4:55 AM
try this codes
CREATE FUNCTION dbo.Split(@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
and run this function as
select * from dbo.split('Chennai,Bangalore,Mumbai',',')
you will get output as,
Chennai
Bangalore
Mumbai
Thank you
Vilas GitePosted Sep 2, 2011, 2:59 AM
Same thread here…
http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/4126a010-6885-4eb0-b79c-c798c90edb85
And, if you want split String in C# then you can refer good one article of Mahesh Sir: http://www.c-sharpcorner.com/UploadFile/mahesh/2890/ ------------------------------------------------------------------------------
If this reply helps your post…then check "This is correct answer"