I am basing this answer off the previous question that you posted. Here is a CTE version that will split the data and them total the declaration:
;with cte (code, DeclarationItem, Declaration) as
(
select Code,
cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
from yourtable
union all
select code,
cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
from cte
where Declaration > ''
),
s2 as
(
select code,
ltrim(rtrim(left(declarationitem, charindex('-', declarationitem)-1))) col1
, reverse(left(reverse(declarationitem), charindex('-', reverse(declarationitem))-1)) col2
from cte
),
fnl as
(
select code, col1,
left(col2, patindex('%[Aa-Zz]%', col2)-1) value,
substring(col2, patindex('%[Aa-Zz]%', col2), len(col2)) str
from s2
)
select code, col1 +' - '+ cast(sum(cast(value as int)) as varchar(50)) + str as declarationitem
from fnl
group by code, col1, str
See SQL Fiddle with Demo
The result is:
| CODE | DECLARATIONITEM |
--------------------------
| 123 | 123 a1 - 102nos |
| 123 | 123 a2 - 230nos |
| 123 | 123 a3 - 11nos |