Hi,@Ebrahim El-Sayed
We can use the BREAK keyword to exit the loop,as Viorel and DanGuzman answered above.
In this situation, we can also modify the WHILE conditional judge statement to achieve the desired result.
Please check this:
IF OBJECT_ID('dbo.udf_test')IS NOT NULL
DROP FUNCTION dbo.udf_test;
GO
CREATE FUNCTION dbo.udf_test(@N bigint)
RETURNS varchar(100)
AS
BEGIN
DECLARE @Chars varchar(100) = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
DECLARE @R varchar(100) = '';
WHILE @N != 0
BEGIN
SET @R = @R + SUBSTRING(@Chars, @N % CAST(0x3e AS int) + 1, 1);
SET @N = @N / CAST(0x3e AS int);
END;
RETURN @R;
END;
GO
SELECT dbo.udf_test(324535354);
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.