If I read your question correctly, you want to avoid hard-coding a substring for each digit in the identifier, for example substring (ID,1,1) + substring (ID,2,1) + ...substring (ID,n,1) . This is inelegant and only works if all of your ID values ββare the same length.
Instead, you can use a recursive CTE. Performing this method works for identifier fields with variable length values.
Disclaimer: this still technically uses substring , but it does not make a clumsy capture of hard code
WITH recur (ID, place, ID_sum) AS ( SELECT ID, 1 , CAST(substring(CAST(ID as varchar),1,1) as int) FROM SO_rbase UNION ALL SELECT ID, place + 1, ID_sum + substring(CAST(ID as varchar),place+1,1) FROM recur WHERE len(ID) >= place + 1 ) SELECT ID, max(ID_SUM) as ID_sum FROM recur GROUP BY ID
source share