What a completely bizarre requirement. However you did provide very easy to consume data!!! A solution here is not really too bad. Just add the next character into your CTE and you then have access to the "next" character.
if object_id('tempdb..#Names') is not null
drop table #Names
if object_id('tempdb..#StringInTable') is not null
drop table #StringInTable
create table #Names (Forename nvarchar(50), Surname nvarchar(50), PhoneticNameKey nvarchar(20))
insert into #Names(Forename, Surname)
select 'JOSE', 'ANTINORI' union all
select 'TEST', 'Knuckle' union all
select 'ITest', 'Nint'
select * from #Names
;with cte as
(
select
substring(Surname, 1, 1) as Chars,
substring(Surname, 2, 1) as Char2,
stuff(Surname, 1, 1, '') as Surname,
1 as RowID
from #Names
union all
select
substring(Surname, 1, 1) as Chars,
substring(Surname, 2, 1) as Char2,
stuff(Surname, 1, 1, '') as data,
RowID + 1 as RowID
from cte
where len(Surname) > 0
)
select RowID, Chars, Char2 into #StringInTable
from cte
order by RowID
select * from #StringInTable
select chars, char2, case chars
when 'A' then 'y'
when 'B' then 'b'
when 'C' then 'k'
when 'D' then 'd'
when 'E' then 'y'
when 'F' then 'f'
when 'G' then 'g'
when 'H' then 'h'
when 'I' then 'y'
when 'J' then 'j'
when 'K' then case when char2 = 'N' then 'n' else 'k' end -- but if K is followed by N then should become n
when 'L' then 'l'
when 'M' then 'm'
when 'N' then case when char2 in ('I', 'T') then 'm' else 'n' end -- if N is followed by I or T then set to m
when 'O' then 'y'
end
from #StringInTable
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/