One way is to use a temporary table, and populate it in a WHILE loop:
declare @letters table (letter varchar(1))
declare @pos int
set @pos = 1
while 1=1
begin
insert into @letters
select substring(name,@pos,1)
from @names
where len(name) >= @pos
if @@rowcount = 0
break
set @pos = @pos + 1
end
select letter, count(*)
from @letters
group by letter
Another way is to create a list of valid character positions in a temporary table, or as in this example, with a recursive common table expression (CTE):
declare @maxLen int
select @maxLen = max(len(name)) from @names
;WITH CharPositions (i) AS (
select 1
union all
select i+1
from CharPositions
where i < @maxLen
)
select substring(n.name,cp.i,1), count(*)
from @names n
inner join CharPositions cp on cp.i <= len(n.name)
group by substring(n.name,cp.i,1)
I've tested the code samples against this dataset:
declare @names table (name varchar(max))
insert into @names values ('abc')
insert into @names values ('def')
insert into @names values ('def')
insert into @names values ('g')
insert into @names values ('g')
insert into @names values ('g')