Thursday, 16 April 2015

Return array from comma separated string in sql server


declare @outputTable TABLE(
dataTable VARCHAR(256)
)
DECLARE @string VARCHAR(MAX)
DECLARE   @delimiter CHAR(1)
set @string='UK-RUD,UK,RJ-BNS,RJ-J,SWM,JH-DEO,OD-BLR,CG-RJN'
set @delimiter= ','
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)

WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @outputTable (dataTable)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END

select * from @outputTable

//result


No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...