;WITH getDigitsCTE AS (
SELECT
Quotient = @Number / 10
,Remainder = @Number % 10
UNION ALL
SELECT
Quotient / 10
, Quotient % 10
FROM getDigitsCTE
WHERE Quotient > 0
)
SELECT
[Sum of digits]=SUM(Remainder)
FROM getDigitsCTE
/*
Sum of digits
---------------
45
*/