create table test( id int identity(1,1) not null primary key , class tinyint not null , name nvarchar(120) not null ) insert into test values (1, N'a'), (1, N'c'), (1, N'c'), (1, N'd'), (1, N'd'), (2, N'd'), (3, N'd'), (3, N'e'), (3, N'f') --For each class, no letter will be repeated select dbo.ConcatDistinct(name + ' '),class from test group by class drop table test