I have this Split function which i got it from some site, now my problem is I am having one table where I want to push in imagespath. For now user can pass two images but in future there might be rise in the number of images required so we have used comma separated column which is a bad practice but was required in this case. Now when retrieve this images I want to segregate those images based on their ids
in database
1 abc.png,xyz.png
2 fgvm.png,ghy.jpg
like I want
Imagepath ID
abc.png 1
xyz.png 1
fgvm.png 2
ghy.jpg 2
Using in the split function I'm doing this
select * from dbo.split ((SELECT ImagePath
FROM dbo.AdvtManagement(nolock)),',')
But this query would give me like this
id val
1 abc.png
2 xyz.png
3 fgvm.png
4 ghy.jpg
Can somebody help me with what I want?
split code
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER 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