Program to find the number in a digit whose positions are divided by 3

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

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

UNION ALL

SELECT
Quotient / 10
, Quotient % 10

FROM getDigitsCTE

WHERE Quotient > 0
)
SELECT
X.Number
FROM
(SELECT
Position = ROW_NUMBER()OVER(ORDER BY Remainder)
,Number = Remainder
FROM getDigitsCTE)X
WHERE X.Position % 3 = 0

/*
Number
-------
3
6
9
*/

Comments or Responses

Login to post response