Here ya go - one way anyway:create table t (ID int, TYPE char(1), NAME varchar(20))insert tselect 1, 'D', 'SMITH' union allselect 1, 'D', 'DOE' union allselect 1, 'P', 'RICH' union allselect 1, 'P', 'GARMIN' union allselect 2, 'D', 'JEN' union allselect 2, 'D', 'DORTHY'gocreate function dbo.getname(@id int, @delim varchar(5), @type char(1)) returns varchar(8000)asbegin declare @out varchar(8000) select @out = coalesce(@out + @delim + name, convert(varchar, @id) + ', ' + name) from t where id = @id and type = @type return @outendgoselect dbo.getname(1, '|', 'D')select dbo.getname(2, '|', 'D')drop function dbo.getnamedrop table tOUTPUT:------------------------1, SMITH|DOE-----------------------2, JEN|DORTHY
Be One with the OptimizerTG