Я уверен, что есть лучший способ сделать это, но следующий код даст вам желаемый результат.
Use AdventureWorks2017;
Go
Declare @xml1 nvarchar(max)
Set @xml1 ='<FeeSize><XMLs><XML Character="1.11" MaxXML="249999.99" Fee="1.60" Cost="1.8" Size="1.8"/><XML Character="251111.11" MaxXML="499999.99" Fee="1.40" Cost="1.7" Size="1.7"/><XML Character="511111.11" MaxXML="999999.99" Fee="1.20" Cost="1.6" Size="1.6"/><XML Character="1111111.11" MaxXML="1999999.99" Fee="1.11" Cost="1.5" Size="1.5"/><XML Character="2111111.11" MaxXML="4999999.99" Fee="1.90" Cost="1.45" Size="1.45"/><XML Character="5111111.11" MaxXML="9999999999999.99" Fee="1.85" Cost="1.425" Size="1.425"/></XMLs></FeeSize>'
Declare @xml2 nvarchar(max)
Set @xml2 = '<FeeSize><XMLs><XML Character="1.11" MaxXML="249999.99" Fee="1.60" Cost="1.8" Size="1.8"/></XMLs></FeeSize>'
If Object_ID ('tempdb..#Six') is not null Drop Table #Six
Create Table #Six (id int identity (1,1), xml_1 nvarchar(max), xml_2 nvarchar(max))
Insert into #Six (xml_1, xml_2)
Select @xml1, @xml2 UNION ALL
Select @xml1, @xml2 UNION ALL
Select @xml1, @xml2 UNION ALL
Select @xml1, @xml2 UNION ALL
Select @xml1, @xml2 UNION ALL
Select @xml1, @xml2
Select case
when id = 1 then charindex('Character',xml_1,1)
when id = 2 and charindex('Character',xml_1,1) <> 0
then charindex('Character',xml_1,charindex('Character',xml_1,1)+1)
when id = 3 and charindex('Character',xml_1,charindex('Character',xml_1,1)+1) <> 0
then charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,1)+1)+1)+1)
when id = 4 and charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,1)+1)+1)+1) <> 0
then charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,1)+1)+1)+1)+1)
when id = 5 and charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,1)+1)+1)+1)+1) <> 0
then charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,1)+1)+1)+1)+1)+1)
when id = 6 and charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,1)+1)+1)+1)+1)+1) <> 0
then charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,charindex('Character',xml_1,1)+1)+1)+1)+1)+1)+1)
ELSE 0 end as cascaded_xml1,
case
when id = 1 then charindex('Character',xml_2,1)
when id = 2 and charindex('Character',xml_2,1) <> 0
then charindex('Character',xml_2,charindex('Character',xml_2,1)+1)
when id = 3 and charindex('Character',xml_2,charindex('Character',xml_2,1)+1) <> 0
then charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,1)+1)+1)+1)
when id = 4 and charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,1)+1)+1)+1) <> 0
then charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,1)+1)+1)+1)+1)
when id = 5 and charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,1)+1)+1)+1)+1) <> 0
then charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,1)+1)+1)+1)+1)+1)
when id = 6 and charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,1)+1)+1)+1)+1)+1) <> 0
then charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,charindex('Character',xml_2,1)+1)+1)+1)+1)+1)+1)
ELSE 0 end as cascaded_xml2
from #Six
То, что происходит в вашем коде, заключается в том, что каскадный символ charindex передается и на самом деле это = 0, что возвращает SQL около начала строки, готовой к поиску нового экземпляра 'Character'. Это довольно сложно обойти. Вышеупомянутый код будет работать, я уверен, однако, что есть лучший способ сделать это.