Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

mysql - Reversing a number in sql using a procedure or function

I have to reverse a number in Sql. I have both PHPmyadmin and Sql management studio, but the following code is not executing. The original code was available here . Thanks

DELIMITER //

CREATE PROCEDURE ggrepeat1()

begin

declare num1 int;

declare num2 int; 

declare rev int default 0;

set @num1:='&num1';

while num1>0 do

set @num2:=num1 mod 10;

set @rev:=num2+(rev*10);

set @num1:=floor(num1/10);

end while;

Select 'Reverse number is: 'rev;

end//

DELIMITER ;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you mean that you want the string representation in base 10 to be reversed -- so 123 becomes 321 -- then just use reverse():

select reverse(x)

If you want this as a number rather than a string, then convert back to a number:

seledt reverse(x) + 0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...