declare @i int = 985;with nums(n) as(select n from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10))nums(n))select sum(convert(int,substring(convert(varchar(10),@i),n,1)))from nums where n <= len(convert(varchar(10),@i))

DECLARE @i BIGINT=99999999999;with nums(n) as( SELECT 1 UNION ALL SELECT n+1 FROM nums WHERE n<30)select sum(convert(int,substring(convert(varchar(30),@i),n,1)))from nums where n <= len(convert(varchar(30),@i));

select n from (values (1),(2),(3),(4)...(50))nums(n)

with nums(n) as( SELECT 1 UNION ALL SELECT n+1 FROM nums WHERE n<LEN(@i))

DECLARE @i varchar(50)='99999999999999999999999999999999999999999999999999';with nums(n) as( SELECT 1 UNION ALL SELECT n+1 FROM nums WHERE n<LEN(@i))select sum(convert(int,substring(convert(varchar(50),@i),n,1)))from nums where n <= len(convert(varchar(50),@i));GO

DECLARE @i varchar(50)='122333444455555';WITH val(x,n) AS( SELECT LEFT(@i,LEN(@i)),'0' UNION ALL SELECT LEFT(x,LEN(x)-1),RIGHT(x,1) FROM val WHERE LEN(x)>0)SELECT SUM(CAST(n AS int)) FROM val

SELECT SUM(CAST(n AS int)) FROM val

SELECT * FROM val

x n-------------------------------------------------- ----122333444455555 012233344445555 51223334444555 5122333444455 512233344445 51223334444 5122333444 412233344 41223334 4122333 412233 31223 3122 312 21 2 1

declare @val varchar(100) ='20876543914578560430730723092317208765439145785604'+'3073072309231720876543914578560430730723092317208'declare @cmd varchar(300)set @cmd = 'select [Sum] = '+ reverse(substring(reverse(replace(replace(replace( replace(replace(replace(replace(replace(replace(replace( convert(varchar(300),@val),'9','9+'),'8','8+'),'7','7+'),'6','6+') ,'5','5+'),'4','4+'),'3','3+'),'2','2+'),'1','1+'),'0','0+')),2,300))print '@val = '+@valprint '@cmd = '+@cmdexec (@cmd)

@val = 208765439145785604307307230923172087654391457856043073072309231720876543914578560430730723092317208@cmd = select [Sum] = 2+0+8+7+6+5+4+3+9+1+4+5+7+8+5+6+0+4+3+0+7+3+0+7+2+3+0+9+2+3+1+7+2+0+8+7+6+5+4+3+9+1+4+5+7+8+5+6+0+4+3+0+7+3+0+7+2+3+0+9+2+3+1+7+2+0+8+7+6+5+4+3+9+1+4+5+7+8+5+6+0+4+3+0+7+3+0+7+2+3+0+9+2+3+1+7+2+0+8 Sum----------- 403(1 row(s) affected)

DECLARE @num BIGINT = 985;WITH Tens(Pos,Val) AS (SELECT 1, CAST(1 AS BIGINT) UNION ALLSELECT 2, CAST(10 AS BIGINT) UNION ALLSELECT 3, CAST(100 AS BIGINT) UNION ALLSELECT 4, CAST(1000 AS BIGINT) UNION ALLSELECT 5, CAST(10000 AS BIGINT) UNION ALLSELECT 6, CAST(100000 AS BIGINT) UNION ALLSELECT 7, CAST(1000000 AS BIGINT) UNION ALLSELECT 8, CAST(10000000 AS BIGINT) UNION ALLSELECT 9, CAST(100000000 AS BIGINT) UNION ALLSELECT 10,CAST(1000000000 AS BIGINT) UNION ALLSELECT 11,CAST(10000000000 AS BIGINT) UNION ALLSELECT 12,CAST(100000000000 AS BIGINT) UNION ALLSELECT 13,CAST(1000000000000 AS BIGINT) UNION ALLSELECT 14,CAST(10000000000000 AS BIGINT) UNION ALLSELECT 15,CAST(100000000000000 AS BIGINT) UNION ALLSELECT 16,CAST(1000000000000000 AS BIGINT) UNION ALLSELECT 17,CAST(10000000000000000 AS BIGINT) UNION ALLSELECT 18,CAST(100000000000000000 AS BIGINT) UNION ALLSELECT 19,CAST(1000000000000000000 AS BIGINT))SELECT SUM((@num / Val) % 10)FROM TensWHERE Val<=@num;