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

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 477
Program to find sum of Even Number of a digit in TSQL

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
SUMOfEVENNumbers = SUM(Remainder)
FROM getDigitsCTE
WHERE Remainder %2 = 0

/*
SUMOfEVENNumbers
-----------------
20
*/

Comments or Responses

Login to post response