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

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...