Program to find sum of Odd Number of a digit in TSQL

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 1098
DECLARE @Number INT = 123456789

;WITH getDigitsCTE AS (
SELECT
SlNo = 1
, Quotient = @Number / 10
,Remainder = @Number % 10

UNION ALL

SELECT
SlNo = SlNo + 1
,Quotient / 10
, Quotient % 10

FROM getDigitsCTE

WHERE Quotient > 0
)

SELECT
SUMOfODDNumbers = SUM(Remainder)
FROM getDigitsCTE
WHERE Remainder %2 <> 0

/*
SUMOfODDNumbers
---------------
25
*/

Comments or Responses

Login to post response