You can use left to compare the first five characters, and you can use for xml path to combine similar lines into one column.
declare @T table ( ID int identity primary key, Col varchar(10) ) insert into @T values ('AAAAAA'), ('AAAAAB'), ('AAAAAC'), ('CCCCCC') select Col, stuff((select ','+T2.Col from @T as T2 where left(T1.Col, 5) = left(T2.Col, 5) and T1.ID <> T2.ID for xml path(''), type).value('.', 'varchar(max)'), 1, 1, '') as Similar from @T as T1
Result:
Col Similar ---------- ------------------------- AAAAAA AAAAAB,AAAAAC AAAAAB AAAAAA,AAAAAC AAAAAC AAAAAA,AAAAAB CCCCCC NULL
source share