Program to find reverse of a Number in TSQL

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 470
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
OriginalNumber=@Number
,ReveredNumber =
STUFF((SELECT '' +
dw.Remainder + ' '
FROM getDigitsCTE dw
ORDER BY dw.SlNo
FOR XML PATH('')),1,0,'')

/*
OriginalNumber ReveredNumber
123456789 987654321
*/

Comments or Responses

Login to post response