Add comma every nth character in value

0

Please login or register to vote for this query.

(click on this box to dismiss)

Psychology and Neuroscience Meta

Q&A about the site for practitioners, researchers, and students in cognitive science, psychology, neuroscience, and psychiatry

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

insert into @T values
('1234567851234'),
('abcdefgh51234')

declare @Len int
set @Len = 8;

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 to main site
loading Hold tight while we fetch your results