Hi I am trying to retrieve values from database. I have a row which has multiple image names separated by “,”. I want to display them in different lines. I am using the following code which is working fine for two values. But even when I have three or more values then too it gives back only two. This is my query:
;with tmp(ImageURL,HeritageId) as
(
select LEFT(ImageURL, CHARINDEX(',',ImageURL+',')-1),
STUFF(ImageURL, 1, CHARINDEX(',',ImageURL+','), '')
from shop.dbo.Images where HeritageId=@HeritageId
union all
select right(ImageURL, CHARINDEX(',',ImageURL+',')-1),
STUFF(ImageURL, 1, CHARINDEX(',',ImageURL+','), '')
from Images
where ImageURL > '' and HeritageId=@HeritageId
)
select ImageURL
from tmp
Your query looks like an attempt to use a recursive CTE to split a string. It that is the case it should look something like this.
Use the CTE in the recursive part instead of the table.