1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | DECLARE @LoginNames VARCHAR ( max ) SET @LoginNames = 'Michael,Rebecca' -- Create a temp table with a single column called LoginName DECLARE @ temp AS TABLE (LoginName NVARCHAR(255)) IF ISNULL (@LoginNames, '' ) <> '' BEGIN DECLARE @s NVARCHAR( max ) WHILE LEN(@LoginNames) > 0 BEGIN IF CHARINDEX( ',' , @LoginNames) > 0 BEGIN SET @s = LTRIM(RTRIM( SUBSTRING (@LoginNames, 1, CHARINDEX( ',' , @LoginNames) - 1))) -- After parsing a single value from the list, insert into the temp table INSERT INTO @ temp (LoginName) VALUES (@s) SET @LoginNames = SUBSTRING (@LoginNames, CHARINDEX( ',' , @LoginNames) + 1, LEN(@LoginNames)) END ELSE BEGIN SET @s = LTRIM(RTRIM(@LoginNames)) -- After parsing a single value from the list, insert into the temp table INSERT INTO @ temp (LoginName) VALUES (@s) SET @LoginNames= '' END END END SELECT LoginName FROM @ temp |
Output:
LoginName |
---|
Michael |
Rebecca |