First you need this function to create (only once):CREATE FUNCTION [dbo].[fnParseArray] (@array VARCHAR(1000),@separator CHAR(1))RETURNS @T Table (occ int,parmValue varchar(50))AS BEGINDECLARE @separator_position INT DECLARE @array_value VARCHAR(1000) declare @occurence int =1 if (left(@array,1)=@separator)begin set @array=stuff(@array,1,1,'')endif (right(@array,1)<>@separator)begin SET @array = @array + @separatorendWHILE PATINDEX('%' + @separator + '%', @array) <> 0 BEGINSELECT @separator_position = PATINDEX('%' + @separator + '%',@array)SELECT @array_value = LEFT(@array, @separator_position - 1)INSERT into @T VALUES (@occurence,@array_value) set @occurence = @occurence + 1SELECT @array = STUFF(@array, 1, @separator_position, '')ENDRETURN END
Then you can do this:Declare @t Table( id int, EmpName nvarchar(500), status char(5), Repair int)Insert Into @tSelect 1,'Arun,Balu','Yes',25 Union AllSelect 2,'Nagu','No',20 Union AllSelect 3,'Mani','No',45 Union AllSelect 4,'XXX,YYY','No',5 --select * from @tselectid,parmValue as EmpName,status,Repairfrom @tcross apply dbo.fnParseArray(EmpName,',')
No, you're never too old to Yak'n'Roll if you're too young to die.