Ever needed a snippet to loop through the comma separated string in SQL Server?
Here is one :
DECLARE @tString AS NVARCHAR(MAX)
SET @
IF SUBSTRING(@tString, LEN(@tString)-1,1)<>','
SET @tString = @tString + ',' --add a comma to the end if it isn't there
SET @nCount = 0
WHILE CHARINDEX(',',@tString) > 0
BEGIN
SET @tSeparatedString = SUBSTRING(@tString,0, CHARINDEX(',',@tString))
SET @tString = SUBSTRING(@tString, CHARINDEX(',',@tString)+1, LEN(@tString) - @nCount) --remove the first item from the list
IF(@tSeparatedString IS NOT NULL AND @tSeparatedString <> '')
BEGIN
-- DO whatever with that separated string
END
No comments:
Post a Comment