Thursday 16 April 2015

Select One by One from comma separated string in sql server query (select multiple table from sql server from comma separated string condition )



DECLARE @string VARCHAR(MAX)
DECLARE   @delimiter CHAR(1)
set @string='RUD,UK-UTT,RJ-BNS,RJ,RJ-SWM,JH-DEO,OD-BLR,RJN,MP-AGR
set @delimiter= ','
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)

WHILE(@start < LEN(@string) + 1)
BEGIN
IF @end = 0
BEGIN
  
SET @end = LEN(@string) + 1
select * from ‘Table’ where Column=SUBSTRING(@string, @start, @end - @start)
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)

END

END

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...