Add comma every nth character in value

0

Please login or register to vote for this query.

(click on this box to dismiss)

Physics

Q&A for active researchers, academics and students of physics

declare @T table
(
  Value nvarchar(100)
);

insert into @T values
('ARAMAU'),
('ARAMAUBEBABRBGCNDKDEEEFOFIFRGEGRIEISITJPYUCAKZKG');

declare @Len int
set @Len = 3;

with Numbers(Number) as
(
  select Number
  from master..spt_values
  where type = 'P'
)
select stuff(T2.X.value('.', 'nvarchar(max)'), 1, 1, '')
from @T as T1
  cross apply (select ','+substring(T1.Value, 1+Number*@Len, @Len)
               from Numbers
               where Number >= 0 and 
                     Number < len(T1.Value) / @Len
               order by Number
               for xml path(''), type) as T2(X)

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results