Add comma every nth character in value


Please login or register to vote for this query.

(click on this box to dismiss)

Psychology & 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

declare @Len int
set @Len = 2;

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

Switch to main site
loading Hold tight while we fetch your results